CmdID

CmdID / PowerShell / Cmdlets / Disconnect

Disconnect-PSSession

Disconnects from a session. PowerShell reference

From the PowerShell reference 9 parameters Alias dnsn

Opens CmdID with your command, read as PowerShell. It is read in your browser and sent nowhere.

What it does

from the PowerShell reference, CC BY 4.0

Disconnects from a session. The Disconnect-PSSession cmdlet 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 connect to the disconnected PSSession from the current session or from another session on the local computer or a different computer. The Disconnect-PSSession cmdlet disconnects only open PSSessions that are connected to the current session. Disconnect-PSSession cannot disconnect broken or closed PSSessions, or interactive PSSessions started by using the Enter-PSSession cmdlet, and it cannot disconnect PSSessions that are connected to other sessions. To reconnect to a disconnected PSSession, use the Connect-PSSession or Receive-PSSession cmdlets. When a PSSession is disconnected, the commands in the PSSession continue to run until they complete, unless the PSSession times out or the commands in the PSSession are blocked by a full output buffer. To change the idle timeout, 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. For more information about the Disconnected Sessions feature, see about_Remote_Disconnected_Sessions. This cmdlet is introduced in Windows PowerShell 3.0.

Disconnect-PSSession [-Session] <PSSession[]> [-IdleTimeoutSec <Int32>] [-OutputBufferingMode <OutputBufferingMode>] [-ThrottleLimit <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
Disconnect-PSSession [-IdleTimeoutSec <Int32>] [-OutputBufferingMode <OutputBufferingMode>] [-ThrottleLimit <Int32>] -Name <String[]> [-WhatIf] [-Confirm] [<CommonParameters>]
Disconnect-PSSession [-IdleTimeoutSec <Int32>] [-OutputBufferingMode <OutputBufferingMode>] [-ThrottleLimit <Int32>] -InstanceId <Guid[]> [-WhatIf] [-Confirm] [<CommonParameters>]
Disconnect-PSSession [-IdleTimeoutSec <Int32>] [-OutputBufferingMode <OutputBufferingMode>] [-ThrottleLimit <Int32>] [-Id] <Int32[]> [-WhatIf] [-Confirm] [<CommonParameters>]

Example 1 - Disconnect a session by name

PS> Disconnect-PSSession -Name UpdateSession Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 UpdateSession Server01 Disconnected Microsoft.PowerShell NoneIdentify

Example 2 - Disconnect a session from a specific computer

PS> Get-PSSession -ComputerName Server12 -Name ITTask | Disconnect-PSSession -OutputBufferingMode Drop -IdleTimeoutSec 86400 Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 ITTask Server12 Disconnected ITTasks NoneIdentify

Example 3 - Using multiple PSSessions on multiple computers

PS> $s = New-PSSession -ComputerName Srv1, Srv2, Srv30 -Name ITTask PS> Invoke-Command $s -FilePath \\Server01\Scripts\Get-PatchStatus.ps1 PS> Get-PSSession -Name ITTask -ComputerName Srv1 | Disconnect-PSSession Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 ITTask Srv1 Disconnected Microsoft.PowerShell None PS> Get-PSSession -ComputerName Srv1, Srv2, Srv30 -Name ITTask Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 ITTask Srv1 Disconnected Microsoft.PowerShell None 2 ITTask Srv2 Opened Microsoft.PowerShell Available 3 ITTask Srv30 Opened Microsoft.PowerShell Available PS> Get-PSSession -ComputerName Srv1 -Name ITTask -Credential Domain01\User01 Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 ITTask Srv1 Disconnected Microsoft.PowerShell None PS> $s = Connect-PSSession -ComputerName Srv1 -Name ITTask -Credential Domain01\User01 PS> Invoke-Command -Session $s {dir $HOME\Scripts\PatchStatusOutput.ps1} PS> Invoke-Command -Session $s {mkdir $HOME\Scripts\PatchStatusOutput} PS> Invoke-Command -Session $s -FilePath \\Server01\Scripts\Get-PatchStatus.ps1 PS> Disconnect-PSSession -Session $sIdentify

Trips 1 Sigma rule: lowUnusually Long PowerShell CommandLine by oscd.community, Natalia Shornikova

Example 4 - Change the timeout value for a PSSession

PS> $Timeout = New-PSSessionOption -IdleTimeout 172800000 PS> $s = New-PSSession -Computer Server01 -Name ITTask -SessionOption $Timeout PS> Disconnect-PSSession -Session $s Disconnect-PSSession : The session ITTask cannot be disconnected because the specified idle timeout value 172800(seconds) is either greater than the server maximum allowed 43200 (seconds) or less that the minimum allowed60(seconds). Choose an idle time out value that is within the allowed range and try again. PS> Invoke-Command -ComputerName Server01 {Get-PSSessionConfiguration Microsoft.PowerShell} | Format-List -Property * Architecture : 64 Filename : %windir%\system32\pwrshplugin.dll ResourceUri : http://schemas.microsoft.com/powershell/microsoft.powershell MaxConcurrentCommandsPerShell : 1000 UseSharedProcess : false ProcessIdleTimeoutSec : 0 xmlns : http://schemas.microsoft.com/wbem/wsman/1/config/PluginConfiguration MaxConcurrentUsers : 5 lang : en-US SupportsOptions : true ExactMatch : true RunAsUser : IdleTimeoutms : 7200000 PSVersion : 3.0 OutputBufferingMode : Block AutoRestart : false SecurityDescriptorSddl : O:NSG:BAD:P(A;;GA;;;BA)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD) MaxMemoryPerShellMB : 1024 MaxIdleTimeoutMs : 2147483647 Uri : http://schemas.microsoft.com/powershell/microsoft.powershell SDKVersion : 2 Name : microsoft.powershell XmlRenderingType : text Capability : {Shell} RunAsPassword : MaxProcessesPerShell : 15 ParentResourceUri : http://schemas.microsoft.com/powershell/microsoft.powershell Enabled : true MaxShells : 25 MaxShellsPerUser : 25 Permission : BUILTIN\Administrators AccessAllowed PSComputerName : localhost RunspaceId : aea84310-6dbf-4c21-90ac-13980039925a PSShowComputerName : True PS> $s.Runspace.ConnectionInfo ConnectionUri : http://Server01/wsman ComputerName : Server01 Scheme : http Port : 80 AppName : /wsman Credential : ShellUri : http://schemas.microsoft.com/powershell/Microsoft.PowerShell AuthenticationMechanism : Default CertificateThumbprint : MaximumConnectionRedirectionCount : 5 MaximumReceivedDataSizePerCommand : MaximumReceivedObjectSize : 209715200 UseCompression : True NoMachineProfile : False ProxyAccessType : None ProxyAuthentication : Negotiate ProxyCredential : SkipCACheck : False SkipCNCheck : False SkipRevocationCheck : False NoEncryption : False UseUTF16 : False OutputBufferingMode : Drop IncludePortInSPN : False Culture : en-US UICulture : en-US OpenTimeout : 180000 CancelTimeout : 60000 OperationTimeout : 180000 IdleTimeout : 172800000 PS> Disconnect-PSSession $s -IdleTimeoutSec 43200 Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 4 ITTask Server01 Disconnected Microsoft.PowerShell None PS> $s.Runspace.ConnectionInfo.IdleTimeout 43200000Identify

Trips 1 Sigma rule: lowUnusually Long PowerShell CommandLine by oscd.community, Natalia Shornikova

Its page in the reference: Disconnect-PSSession.

Parameters

9 in the PowerShell reference
-IdDisconnects from sessions with the specified session ID. 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 the Get-PSSession cmdlet. The instance ID is stored in the Id property of the session.
-IdleTimeoutSecChanges the idle timeout value of the disconnected PSSession. Enter a value in seconds. The minimum value is 60 (1 minute). The idle timeout determines how long the disconnected PSSession is maintained on the remote computer. When the timeout expires, the PSSession is deleted. Disconnected PSSessions 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 timeout 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 timeout 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).
-InstanceIdDisconnects from sessions with the specified instance IDs. 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 the Get-PSSession cmdlet. The instance ID is stored in the InstanceId property of the session.
-NameDisconnects from sessions with the specified friendly names. Wildcards are permitted. To get the friendly name of a session, use the Get-PSSession cmdlet. The friendly name is stored in the Name property of the session.
-OutputBufferingModeDetermines 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. Valid values 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 discarded. - None: No output buffering mode is specified. The value of the OutputBufferingMode property of the session configuration is used for the disconnected session.
-SessionDisconnects from the specified PSSessions. Enter PSSession objects, such as those that the New-PSSession cmdlet returns. You can also pipe a PSSession object to Disconnect-PSSession. The Get-PSSession cmdlet can get all PSSessions that terminate at a remote computer, including PSSessions that are disconnected and PSSessions that are connected to other sessions on other computers. Disconnect-PSSession disconnects only PSSession that are connected to the current session. If you pipe other PSSessions to Disconnect-PSSession, the Disconnect-PSSession command fails.
-ThrottleLimitSets 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.
-ConfirmPrompts you for confirmation before running the cmdlet.
-WhatIfShows what would happen if the cmdlet runs. The cmdlet is not run.