Remove-PSSession
Use Remove-PSSession to close a PSSession and release the resources it holds on the remote computer.
Documentation: Microsoft Learn — PowerShell module reference
Syntax
See the command anatomy
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Remove-PSSession [-Session] <PSSession[]> [-WhatIf] [-Confirm] [<CommonParameters>]- Stage 1Remove-PSSession
-Session -WhatIf -Confirm
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Follow the objects
Run the command to inspect each stage.
Examples
Get-PSSession -WhatIf
Show reference output
Sample output · fictional data, shaped like the real thing
Id Name ComputerName ComputerUri State ConfigurationName Availability -- ---- ------------ ----------- ----- ----------------- ------------
Remove-PSSession -ComputerName SRV01
Show reference output
Sample output · fictional data, shaped like the real thing
PSComputerName Result -------------- ------ SRV01 Operation completed
Select Id, ComputerName, State
Show reference output
Sample output · fictional data, shaped like the real thing
Id ComputerName State -- ------------ ------ 1 SRV01 Opened
Parameters
Parameter definitions:
-Session-WhatIf-Confirm
Practical use and troubleshooting
Practical scenario: Use Remove-PSSession when you need to close a PSSession and release the resources it holds on the remote computer. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.
Common error and fix: An already-removed session can produce a lookup failure. Use cleanup on the retained session object and do not remove sessions that another part of the script still needs. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.
See also: Get-PSSession. Get-PSSession inspects remoting session state; Remove-PSSession closes selected sessions. Closing a session also affects remote state held inside it. Read the linked command before substituting it; similar names do not imply identical inputs or output.
Recap
Remove-PSSession Use Remove-PSSession to close a PSSession and release the resources it holds on the remote computer.
Next: Enter-PSSession