Export (0) Print
Expand All

Remove-PSSession

Updated: August 9, 2015

Remove-PSSession

Closes one or more Windows PowerShell sessions (PSSessions).

Aliases

The following abbreviations are aliases for this cmdlet: 

  • rsn

Syntax

Parameter Set: Id
Remove-PSSession [-Id] <Int32[]> [-Confirm] [-WhatIf] [ <CommonParameters>]

Parameter Set: ComputerName
Remove-PSSession [-ComputerName] <String[]> [-Confirm] [-WhatIf] [ <CommonParameters>]

Parameter Set: InstanceId
Remove-PSSession -InstanceId <Guid[]> [-Confirm] [-WhatIf] [ <CommonParameters>]

Parameter Set: Name
Remove-PSSession -Name <String[]> [-Confirm] [-WhatIf] [ <CommonParameters>]

Parameter Set: Session
Remove-PSSession [-Session] <PSSession[]> [-Confirm] [-WhatIf] [ <CommonParameters>]




Detailed 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, 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[]>

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 (.).


Aliases

Cn

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Id<Int32[]>

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.


Aliases

none

Required?

true

Position?

1

Default Value

None

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-InstanceId<Guid[]>

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.


Aliases

none

Required?

true

Position?

named

Default Value

None

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Name<String[]>

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.


Aliases

none

Required?

true

Position?

named

Default Value

None

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Session<PSSession[]>

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.


Aliases

none

Required?

true

Position?

1

Default Value

None

Accept Pipeline Input?

true (ByValue, ByPropertyName)

Accept Wildcard Characters?

false

-Confirm

Prompts you for confirmation before running the cmdlet.


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-WhatIf

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


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see    about_CommonParameters.

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • System.Management.Automation.Runspaces.PSSession

    You can pipe a session object to this cmdlet.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • None

    This cmdlet does not return any objects.


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.

Examples

Example 1: Remove sessions by using IDs

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


PS C:\> Remove-PSSession -Id 1, 2

Example 2: Remove all the sessions in the current session

These commands 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

- or -

PS C:\> Remove-PSSession -Session (Get-PSSession)

- or -

PS C:\> $s = Get-PSSession
PS C:\> Remove-PSSession -Session $s

Example 3: Close sessions by using names

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


PS C:\> $r = Get-PSSession -ComputerName Serv*
PS C:\> $r | Remove-PSSession

Example 4: Close sessions connected to a port

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.


PS C:\> Get-PSSession | where {$_.port -eq 90} | Remove-PSSession

Example 5: Close a session based on instance ID

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.


PS C:\> Get-PSSession | Format-Table ComputerName, InstanceID  -AutoSize
PS C:\> Remove-PSSession -InstanceID fc4e9dfa-f246-452d-9fa3-1adbdd64ae85

Example 6: Create a function that deletes all sessions in the current session

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.


PS C:\> Function EndPSS { Get-PSSession | Remove-PSSession }

Related topics

Community Additions

ADD
Show:
© 2016 Microsoft