CmdID / PowerShell / Cmdlets / Remove
Remove-PSSession
Closes one or more PowerShell sessions (PSSessions). PowerShell reference
From the PowerShell reference
10 parameters
Alias rsn
What it does
Closes one or more PowerShell sessions (PSSessions). The Remove-PSSession cmdlet closes PowerShell sessions (PSSessions) in the current session. It stops any commands that are running in the PSSessions, ends the PSSession, and releases the resources that the PSSession was using. If the PSSession is connected to a remote computer, this cmdlet also closes the connection between the local and remote computers. To remove a PSSession, enter the Name, ComputerName, Id, or InstanceId of the session. If you have saved the PSSession in a variable, the session object remains in the variable, but the state of the PSSession is Closed.
Remove-PSSession [-Id] <Int32[]> [-WhatIf] [-Confirm] [<CommonParameters>]Remove-PSSession [-Session] <PSSession[]> [-WhatIf] [-Confirm] [<CommonParameters>]Remove-PSSession -ContainerId <String[]> [-WhatIf] [-Confirm] [<CommonParameters>]Remove-PSSession -VMId <Guid[]> [-WhatIf] [-Confirm] [<CommonParameters>]Remove-PSSession -VMName <String[]> [-WhatIf] [-Confirm] [<CommonParameters>]Remove-PSSession -InstanceId <Guid[]> [-WhatIf] [-Confirm] [<CommonParameters>]Remove-PSSession -Name <String[]> [-WhatIf] [-Confirm] [<CommonParameters>]Remove-PSSession [-ComputerName] <String[]> [-WhatIf] [-Confirm] [<CommonParameters>]
Remove sessions by ID
Remove-PSSession -Id 1, 2IdentifyRemove all the sessions in the current session
Get-PSSession | Remove-PSSession
Remove-PSSession -Session (Get-PSSession)
$s = Get-PSSession
Remove-PSSession -Session $sIdentifyClose sessions by name
$r = Get-PSSession -ComputerName Serv*
$r | Remove-PSSessionIdentifyClose sessions connected to a port
Get-PSSession | where {$_.Port -eq 90} | Remove-PSSessionIdentifyClose a session by instance ID
Get-PSSession | Format-Table ComputerName, InstanceId -AutoSizeIdentifyCreate a function that deletes all sessions in the current session
function EndPSS { Get-PSSession | Remove-PSSession }IdentifyIts page in the reference: Remove-PSSession.
Parameters
-ComputerNameSpecifies an array of names of computers. This cmdlet closes the PSSessions that are connected to the specified computers. Wildcard characters are permitted.-ContainerIdSpecifies an array of IDs of containers. This cmdlet removes sessions for each of the specified containers. Use the docker ps command to get a list of container IDs. For more information, see the help for the docker ps command.-IdSpecifies an array of IDs of sessions. This cmdlet closes the PSSessions with the specified IDs. Type one or more IDs, separated by commas, or use the range operator (..) to specify a range of IDs.
An ID is an integer that uniquely identifies the PSSession in the current session. It is easier to remember and type than the InstanceId, but it is unique only in the current session. To find the ID of a PSSession, run the Get-PSSession cmdlet without parameters.-InstanceIdSpecifies an array of instance IDs. This cmdlet closes the PSSessions that have the specified instance IDs.
The instance ID is a GUID that uniquely identifies a PSSession in the current session. The instance ID is unique, even when you have multiple sessions running on a single computer.
The instance ID is stored in the InstanceId property of the object that represents a
PSSession. To find the InstanceId of the PSSessions in the current session, type
Get-PSSession | Format-Table Name, ComputerName, InstanceId.-NameSpecifies an array of friendly names of sessions. This cmdlet closes the PSSessions that have the specified friendly names. Wildcard characters are permitted.
Because the friendly name of a PSSession might not be unique, when you use the Name parameter, consider also using the WhatIf or Confirm parameter in the Remove-PSSession command.-SessionSpecifies the session objects of the PSSessions to close. Enter a variable that contains the
PSSessions or a command that creates or gets the PSSessions, such as a New-PSSession or
Get-PSSession command. You can also pipe one or more session objects to Remove-PSSession.-VMIdSpecifies an array of ID of virtual machines. This cmdlet starts an interactive session with each of the specified virtual machines. To see the virtual machines that are available to you, use the following command:
Get-VM | Select-Object -Property Name, ID-VMNameSpecifies an array of names of virtual machines. This cmdlet starts an interactive session with each
of the specified virtual machines. To see the virtual machines that are available to you, use the
Get-VM cmdlet.-ConfirmPrompts you for confirmation before running the cmdlet.-WhatIfShows what would happen if the cmdlet runs. The cmdlet is not run.