Disconnect PSSession - PowerShell


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.


Disconnect-PSSession [-Session <PSSession>] [-Force] [-Confirm] [-WhatIf] [<CommonParameters>]


| 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 |


Example 1: Disconnect the current session


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.


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