Skip to content
PowerShell Commands

Remove-PSSession

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

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, 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.

Parameters

-ComputerName <String[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies an array of names of computers. This cmdlet closes the PSSessions that are connected to the specified computers. Wildcard characters are permitted.

Type the NetBIOS name, an IP address, or a fully qualified domain name of one or more remote computers. To specify the local computer, type the computer name, localhost, or a dot (.).

-Confirm [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Prompts you for confirmation before running the cmdlet.

-ContainerId <String[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies an array of IDs of containers. This cmdlet starts an interactive session with each of the specified containers. To see the containers that are available to you, use the Get-Container cmdlet.

-Id <Int32[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies 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.

-InstanceId <Guid[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies 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`.

-Name <String[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies 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.

-Session <PSSession[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies 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 .

-VMId <Guid[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies 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`

-VMName <String[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies 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.

-WhatIf [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Shows what would happen if the cmdlet runs. The cmdlet is not run.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs
System.Management.Automation.Runspaces.PSSession
You can pipe a session object to this cmdlet.
Outputs
None
This cmdlet does not return any objects.
Examples
  1. Remove sessions by using IDs:
    PS C:\> Remove-PSSession -Id 1, 2
    

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

  2. Remove all the sessions in the current session:
    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.

  3. Close sessions by using names:
    PS C:\> $r = Get-PSSession -ComputerName Serv*
    PS C:\>  $r | Remove-PSSession
    

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

  4. Close sessions connected to a port:
    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 .

  5. Close a session based on instance ID:
    PS C:\> Get-PSSession | Format-Table ComputerName, InstanceID  -AutoSize
    
       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, or 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, which formats their ComputerName and InstanceID properties in a table. The AutoSize parameter compresses the columns for display.

    From the resulting display, you 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.

  6. Create a function that deletes all sessions in the current session:
    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, type `EndPSS`.
Additional Notes
 The Id parameter is mandatory. To delete all the PSSessions * in the current session, type `Get-PSSession | 
 Remove-PSSession`. A PSSession uses a persistent connection to a remote computer. Create a PSSession * to run 
 a series of commands that share data. For more information, type `Get-Help about_PSSessions`.  PSSessions are 
 specific to the current session. When you end a session, the PSSessions * that you created in that session are 
 forcibly closed.

This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.

PowerShell Commands