Disconnect from a session (an interactive session with a remote computer).

      Disconnect-PSSession [-Id] Int32[] [-IdleTimeoutSec Int32] [-Confirm]
         [-OutputBufferingMode {None | Drop | Block}] [-ThrottleLimit Int32] [-WhatIf] [CommonParameters]
      Disconnect-PSSession -InstanceId Guid[] [-IdleTimeoutSec Int32] [-Confirm] 
         [-OutputBufferingMode {None | Drop | Block}] [-ThrottleLimit Int32] [-WhatIf] [CommonParameters]
      Disconnect-PSSession -Name String[] [-IdleTimeoutSec Int32] [-Confirm]
         [-OutputBufferingMode {None | Drop | Block}] [-ThrottleLimit Int32] [-WhatIf] [CommonParameters]
      Disconnect-PSSession [-Session] PSSession[] [-IdleTimeoutSec Int32] [-Confirm]
         [-OutputBufferingMode {None | Drop | Block}] [-ThrottleLimit Int32] [-WhatIf] [CommonParameters]

   -Id int
       An array of IDs of sessions that this cmdlet disconnects. Type one or more IDs, separated by commas,
       or use the range operator (..) to specify a range of IDs
       To get the ID of a session, use Get-PSSession, the instance ID is stored in the ID property.

       Change the idle time-out value of the disconnected PSSession.
       Enter a value in seconds. The minimum value is 60 (1 minute).
       The idle time-out determines how long the disconnected PSSession is maintained on the remote computer.
       When the time-out expires, the PSSession is deleted.

       Disconnected PSSession objects are considered to be idle from the moment that they are disconnected,
       even if commands are running in the disconnected session.
       The default value for the idle time-out of a session is set by the value of the IdleTimeoutMs property
       of the session configuration. The default value is 7200000 milliseconds (2 hours).

       The value of this parameter takes precedence over the value of the IdleTimeout property of the
       $PSSessionOption preference variable and the default idle time-out value in the session configuration.
       However, this value cannot exceed the value of the MaxIdleTimeoutMs property of the session configuration.
       The default value of MaxIdleTimeoutMs is 12 hours (43200000 milliseconds).

   -InstanceId Guid
       An array of session GUIDs that this cmdlet will disconnect.
       The instance ID is a GUID that uniquely identifies a session on a local or remote computer.
       The instance ID is unique, even across multiple sessions on multiple computers.
       To get the instance ID of a session, use Get-PSSession, the instance ID is stored in the InstanceID property.

   -Name string
       The friendly name of an existing session to disconnect. Wildcard characters are permitted.
       To get the friendly name of a session, use Get-PSSession.

       Specifies how command output is managed in the disconnected session when the output buffer is full.
       The default value is Block.
       If the command in the disconnected session is returning output and the output buffer fills, the value of this 
       parameter effectively determines whether the command continues to run while the session is disconnected.
       A value of Block suspends the command until the session is reconnected. A value of Drop allows the command to 
       complete, although data might be lost. When using the Drop value, redirect the command output to a file on disk.
        The acceptable values for this parameter are:
        - Block. When the output buffer is full, execution is suspended until the buffer is clear.
        - Drop. When the output buffer is full, execution continues. As new output is saved, the oldest output is 
        - None. No output buffering mode is specified. The value of the OutputBufferingMode property of the session 
        configuration is used for the disconnected session.

   -Session PSSession
       An array of sessions. Enter PSSession objects, such as those that the New-PSSession cmdlet returns. 
       You can also pipe a PSSession object to Disconnect-PSSession.
       Get-PSSession can get all PSSession objects that end at a remote computer. These include PSSession objects
       that are disconnected and PSSession objects that are connected to other sessions on other computers.

       Disconnect-PSSession will only disconnect PSSessions that are connected to the current session.
       If you pipe other PSSession objects to Disconnect-PSSession , the Disconnect-PSSession command will fail.

       Set the throttle limit for the Disconnect-PSSession command.
       The throttle limit is the maximum number of concurrent connections that can be established to run this 
       command. If you omit this parameter or enter a value of 0, the default value, 32, is used.
       The throttle limit applies only to the current command, not to the session or to the computer.
   -WhatIf []
       Shows what would happen if the cmdlet runs. The cmdlet is not run.

       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer, -PipelineVariable, -OutVariable.

Standard Aliases for Disconnect-PSSession: dnsn

This Disconnect-PSSession cmdlet was introduced in Windows PowerShell 3.0

Disconnect-PSSession disconnects a PowerShell session ( PSSession ), such as one started by using the New-PSSession cmdlet, from the current session. As a result, the PSSession is in a disconnected state.
You can then connect to the disconnected PSSession from the current session or from another session on the local computer or a different computer.

Disconnect-PSSession disconnects only open PSSessions that are connected to the current session. Disconnect-PSSession cannot disconnect broken or closed PSSession objects, or interactive PSSession objects started by Enter-PSSession, and it cannot disconnect PSSession objects that are connected to other sessions.

The idle time-out property of a session is critical to disconnected sessions, because it determines how long a disconnected session is maintained before it is deleted.

To reconnect to a disconnected PSSession, use Connect-PSSession or Receive-PSSession.

When a PSSession is disconnected, the commands in the PSSession continue to run until they finish, unless the PSSession times out or the commands in the PSSession are blocked by a full output buffer. To change the idle time-out, use the IdleTimeoutSec parameter. To change the output buffering mode, use the OutputBufferingMode parameter. You can also use the InDisconnectedSession parameter of the Invoke-Command cmdlet to run a command in a disconnected session.

To end the interactive session and disconnect from the remote computer, use Exit-PSSession, or type "exit".


Disconnect a session:

PS C:> Disconnect-PSSession -Name ss64Session

Disconnect a PSSession that is running on the Server64 computer in the current session:

PS C:> Get-PSSession -ComputerName Server64 -Name DemoSession | Disconnect-PSSession -OutputBufferingMode Drop -IdleTimeoutSec 86400

Setting the OutputBufferingMode parameter to set the output mode to Drop makes sure that the script that is running in the session can continue to run even if the session output buffer is full.
Because the script writes its output to a report on a file share, other output can be lost without consequence.

The IdleTimeoutSec parameter extends the idle time-out of the session to 24 hours. Thisallows time to reconnect to the session to verify that the script ran and troubleshoot if needed.

Disconnect multiple sessions in an enterprise scenario:

PS C:> $s = New-PSSession -ComputerName Srv1, Srv2, Srv30 -Name ITTask
PS C:> Invoke-Command $s -FilePath \\Server64\Scripts\Get-PatchStatus.ps1
PS C:> Get-PSSession -Name ITTask -ComputerName Srv1 | Disconnect-PSSession
PS C:> Get-PSSession -ComputerName Srv1, Srv2, Srv30 -Name ITTask

This series of commands shows how Disconnect-PSSession might be used in an enterprise scenario. In this case, a new technician starts a script in a session on a remote computer and runs into a problem. The technician disconnects from the session so that a more experienced manager can connect to the session and resolve the problem.

“And he could go on in life, existing from day to day, without connection and without hope. For he did not know what to do with himself” - D.H. Lawrence, Lady Chatterley's Lover.

Related PowerShell Cmdlets:

Connect-PSSession - Reconnect to a disconnected session.
Enter-PSSession - Start an interactive session with a remote computer.
Exit-PSSession - End an interactive session with a remote computer.
New-PSSession - Create a persistent connection to a local or remote computer.
Remove-PSSession - Close PowerShell sessions (non-interactive).
Get-PSSessionConfiguration - Get the registered PS session configuration including the maximum permitted **IdleTimeout**.

Copyright © SS64.com 1999-2020
Some rights reserved