Remove-PSSession

Closes one or more PowerShell sessions (PSSessions).

语法

Id (默认值)

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

Session

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

ContainerId

Remove-PSSession
    -ContainerId <String[]>
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

VMId

Remove-PSSession
    -VMId <Guid[]>
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

VMName

Remove-PSSession
    -VMName <String[]>
    [-WhatIf]
    [-Confirm]
    [<CommonParameters>]

InstanceId

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

Name

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

ComputerName

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

说明

The Remove-PSSession cmdlet closes 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.

示例

Example 1: Remove sessions by ID

Remove-PSSession -Id 1, 2

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

Example 2: Remove all the sessions in the current session

Get-PSSession | Remove-PSSession
Remove-PSSession -Session (Get-PSSession)
$s = Get-PSSession
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.

Example 3: Close sessions by name

$r = Get-PSSession -ComputerName Serv*
$r | Remove-PSSession

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

Example 4: Close sessions connected to a port

Get-PSSession | Where-Object {$_.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.

Example 5: Close a session by instance ID

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

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

function EndPSS { Get-PSSession | Remove-PSSession }

This function closes every PSSession in the current session. After you add this function to your PowerShell profile, to delete all sessions, type EndPSS.

参数

-ComputerName

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

参数属性

类型:

String[]

默认值:None
支持通配符:True
不显示:False
别名:Cn

参数集

ComputerName
Position:0
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-Confirm

Prompts you for confirmation before running the cmdlet.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:cf

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-ContainerId

Specifies an array of IDs of containers. This cmdlet removes sessions for each of the specified containers. Use the docker ps command to get a list of container IDs. For more information, see the help for the docker ps command.

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False

参数集

ContainerId
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-Id

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.

参数属性

类型:

Int32[]

默认值:None
支持通配符:False
不显示:False

参数集

Id
Position:0
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-InstanceId

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.

参数属性

类型:

Guid[]

默认值:None
支持通配符:False
不显示:False

参数集

InstanceId
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-Name

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.

参数属性

类型:

String[]

默认值:None
支持通配符:True
不显示:False

参数集

Name
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-Session

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.

参数属性

类型:

PSSession[]

默认值:None
支持通配符:False
不显示:False

参数集

Session
Position:0
必需:True
来自管道的值:True
来自管道的值(按属性名称):True
来自剩余参数的值:False

-VMId

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

参数属性

类型:

Guid[]

默认值:None
支持通配符:False
不显示:False
别名:VMGuid

参数集

VMId
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-VMName

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.

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False

参数集

VMName
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-WhatIf

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

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:wi

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

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

输入

PSSession

You can pipe a session object to this cmdlet.

输出

None

This cmdlet returns no output.

备注

PowerShell includes the following aliases for Remove-PSSession:

  • All platforms:

    • rsn
  • 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.