PowerShell Logo Small

Remove-PSSession



This is the built-in help made by Microsoft for the command 'Remove-PSSession', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Closes one or more Windows PowerShell sessions (PSSessions).

SYNTAX


Remove-PSSession [-Id] <Int32[]> [-Confirm] [-WhatIf] [<CommonParameters>]
Remove-PSSession [-ComputerName] <String[]> [-Confirm] [-WhatIf] [<CommonParameters>]
Remove-PSSession -InstanceId <Guid[]> [-Confirm] [-WhatIf] [<CommonParameters>]
Remove-PSSession -Name <String[]> [-Confirm] [-WhatIf] [<CommonParameters>]
Remove-PSSession [-Session] <PSSession[]> [-Confirm] [-WhatIf] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Remove-PSSession cmdlet closes Windows 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, Remove-PSSession 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."



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=289608
Connect-PSSession
Disconnect-PSSession
Enter-PSSession
Exit-PSSession
Get-PSSession
Invoke-Command
New-PSSession
Receive-PSSession
about_PSSessions
about_Remote

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>remove-pssession -id 1, 2



This command removes the PSSessions that have IDs 1 and 2.










-------------------------- EXAMPLE 2 --------------------------

PS C:\>get-pssession | remove-pssession

- or -

PS C:\>remove-pssession -session (get-pssession)

- or -

PS C:\>$s = get-pssession
PS C:\>remove-pssession -session $s



These commands remove all of the PSSessions in the current session. Although the three command formats look different, they have the same effect.










-------------------------- EXAMPLE 3 --------------------------

PS C:\>$r = get-pssession -computername Serv*
$r | remove-pssession



These commands close the PSSessions that are connected to computers that have names that begin with "Serv".










-------------------------- EXAMPLE 4 --------------------------

PS C:\>get-pssession | where {$_.port -eq 90} | remove-pssession



This command closes the PSSessions that are connected to port 90. You can use this command format to identify PSSessions by properties other than ComputerName, Name,
InstanceID, and ID.










-------------------------- EXAMPLE 5 --------------------------

PS C:\>get-pssession | ft computername, instanceID -auto

ComputerName InstanceId
------------ ----------------
Server01 875d231b-2788-4f36-9f67-2e50d63bb82a
localhost c065ffa0-02c4-406e-84a3-dacb0d677868
Server02 4699cdbc-61d5-4e0d-b916-84f82ebede1f
Server03 4e5a3245-4c63-43e4-88d0-a7798bfc2414
TX-TEST-01 fc4e9dfa-f246-452d-9fa3-1adbdd64ae85

PS C:\>remove-pssession -InstanceID fc4e9dfa-f246-452d-9fa3-1adbdd64ae85



These commands show how to close a PSSession based on its instance ID (RemoteRunspaceID).

The first command uses the Get-PSsession cmdlet to get the PSSessions in the current session. It uses a pipeline operator (|) to send the PSSessions to the Format-Table
cmdlet (alias: ft), which formats their ComputerName and InstanceID properties in a table. The AutoSize parameter ("auto") compresses the columns for display.

From the resulting display, the administrator can identify the PSSession to be closed, and copy and paste the InstanceID of that PSSession into the second command.

The second command uses the Remove-PSSession cmdlet to remove the PSSession with the specified instance ID.










-------------------------- EXAMPLE 6 --------------------------

PS C:\>function EndPSS { get-pssession | remove-pssession }



This function deletes all of the PSSessions in the current session. After you add this function to your Windows Powershell profile, to delete all sessions, just type
"endpss".