Remove-PSSession

Close one or more PowerShell sessions (PSSessions).

Syntax
      Remove-PSSession [[-ComputerName] string[]] [-Confirm] [-WhatIf] [CommonParameters]

      Remove-PSSession [-Id] Int32[] [-Confirm] [-WhatIf] [CommonParameters]

      Remove-PSSession [-InstanceId Guid[]] [-Confirm] [-WhatIf] [CommonParameters]

      Remove-PSSession [-Name string[]] [-Confirm] [-WhatIf] [CommonParameters]

      Remove-PSSession [-Session] PSSession[]] [-Confirm] [-WhatIf] [CommonParameters]

Key
   -ComputerName string[]
       Close the PSSessions that are connected to the specified computers. Wildcards 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 (.).

   -Id Int32[]
Close the PSSessions with the specified IDs. Type one or more IDs (separated by commas) or use the range operator (..) 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 within the current session. To find the ID of a PSSession, use Get-PSSession without parameters. -InstanceId Guid[]
Close the PSSessions with the specified instance IDs. The instance ID is a GUID that uniquely identifies a PSSession in the current session. The InstanceID is unique, even when you have multiple sessions running on a single computer. The InstanceID is stored in the InstanceID property of the object that represents a PSSession. To find the InstanceID of the PSSessions in the current session: "Get-PSSession | format-table Name, ComputerName, InstanceId". -Name string[] A friendly name for the PSSession. Use this name to refer to the PSSession when using other cmdlets, such as Get-PSSession and Enter-PSSession. The name is not required to be unique to the computer or the current session. -Session PSSession[] Use the specified PSSession as a model for the new PSSession. This parameter create a new PSSession with the same properties as the specified PSSession. Enter a variable that contains the PSSessions or a command that creates or gets the PSSessions, such as New-PSSession or Get-PSSession. The resulting PSSessions have the same computer name, application name, connection URI, port, configuration name, throttle limit, and Secure Sockets Layer (SSL) value as the originals, but they have a different display name, ID, and instance ID (GUID).

Standard Aliases for Remove-PSSession: rsn

Remove-PSSession closes PowerShell sessions (PSSessions) in the current session. It stops any commands that are running in the PSSession, 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."

Examples

Remove the PSSessions that have IDs 1 and 2.:

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

Remove all of the PSSessions in the current session. Although the three command formats look different, they have the same effect:

PS C:> get-pssession | remove-pssession 

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

PS C:> $sess = get-pssession
PS C:> remove-pssession -session $sess

Close the PSSessions that are connected to computers that have names that begin with "ss64":

PS C:> $r = get-pssession -computername ss64*

PS C:> $r | remove-pssession

Closes the PSSessions that are connected to port 90:

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

Close a PSSession based on its instance ID:

PS C:> remove-pssession -InstanceID fc3e6dfb-f342-253d-7fa4-1abdfc64ae84

Create a function that deletes all the PSSessions in the current session.:

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

“It is the final proof of God’s omnipotence that he need not exist in order to save us” ~ Peter De Vries, (The Mackerel Plaza)

Related PowerShell Cmdlets

Disconnect-PSSession - Disconnect from a session.
New-PSSession - Create a persistent connection to a local or remote computer.
Enter-PSSession - Start an interactive session with a remote computer.
Exit-PSSession - End an interactive session with a remote computer.


 
Copyright © 1999-2024 SS64.com
Some rights reserved