Disconnect PSSession - PowerShell
Overview
Disconnect-PSSession terminates an active PowerShell session established with a remote computer or resource. It allows you to close the session, terminating any active commands or tasks running on the remote system.
Syntax
Disconnect-PSSession [-Session <PSSession>] [-Force] [-Confirm] [-WhatIf] [<CommonParameters>]
Options/Flags
| Option | Description | Default |
|—|—|—|
| -Session | Specifies the session to be disconnected. If omitted, the current session is disconnected. | Current session |
| -Force | Forces the disconnection without any confirmation prompt. | False |
| -Confirm | Prompts for confirmation before disconnecting the session. | True |
| -WhatIf | Performs a simulation without actually disconnecting the session. | False |
Examples
Example 1: Disconnect the current session
Disconnect-PSSession
Example 2: Forcefully disconnect a specific session
Disconnect-PSSession -Session MySession -Force
Example 3: Perform a simulation before disconnecting
Disconnect-PSSession -WhatIf
Common Issues
- Disconnected session: If the remote computer or resource becomes unavailable while the session is active, it may lead to a disconnected session.
Integration
Combine with Get-PSSession
Get a list of active sessions before disconnecting:
Get-PSSession | Disconnect-PSSession
Use in scripts
Automate the disconnection of multiple sessions:
$sessions = Get-PSSession
foreach ($session in $sessions) {
Disconnect-PSSession -Session $session
}
Related Commands
- Enter-PSSession: Establish a new PowerShell session with a remote system.
- Get-PSSession: List active PowerShell sessions.
- PowerShell Remoting documentation