PowerShell Genie

Remove-PSSession

Use Remove-PSSession to close a PSSession and release the resources it holds on the remote computer.

Command 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — PowerShell module reference

Syntax

1PS> Remove-PSSession [-Session] <PSSession[]> [-WhatIf] [-Confirm] [<CommonParameters>]
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>]
  1. Stage 1Remove-PSSession-Session -WhatIf -Confirm

    Try it

    Windows PowerShell

    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.

    Interactive simulation · fictional data

    Examples

    Get-PSSession -WhatIf

    1PS> Get-PSSession | Remove-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

    1PS> $s = New-PSSession -ComputerName SRV01
    2PS> try { Invoke-Command -Session $s { Get-Service Spooler } } finally { Remove-PSSession $s }
    Show reference output

    Sample output · fictional data, shaped like the real thing

    PSComputerName  Result
    --------------  ------
    SRV01           Operation completed

    Select Id, ComputerName, State

    1PS> Get-PSSession | Select-Object 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

    PowerShell module reference on Microsoft Learn

    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.

    Remove-PSSession reference on Microsoft Learn

    Recap

    Remove-PSSession Use Remove-PSSession to close a PSSession and release the resources it holds on the remote computer.

    Next: Enter-PSSession
    Genie guideRemove-PSSession

    Use Remove-PSSession to close a PSSession and release the resources it holds on the remote computer. The console below is pre-loaded — run it, then change a value and run it again.