PowerShell 2.0: One Cmdlet at a Time 37 Remove-PSSession

Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Remove-PSSession cmdlet.

What can I do with it?

Close a remote PowerShell session which is open in the current session.

Examples:

Establish a persistent remote PowerShell connection to Test01 using New-PSSession , return the results for which services begin with T, then remove that session. Finally confirm the session has been removed by running Get-PSSession and seeing no results.

New-PSSession -ComputerName Test01 Invoke-Command -Session (Get-PSSession -id 1) -ScriptBlock {Get-Service | Where-Object {$_.name -like ‘T*’}} Remove-PSSession -Id 1

An interesting thing to note is that if you store the session in a variable and then remove the session you will see that the State changes to Closed.

Establish a persistent remote PowerShell connection to Test01 using New-PSSession and store that in the variable $session1, confirm the session is running via Get-PSSession, then remove that session. Now examine $session1 and note the State as Closed.

$session1 = New-PSSession -ComputerName Test01 Get-PSSession Remove-PSSession -Id 1 $session1

How could I have done this in PowerShell 1.0?

Remoting did not exist in PowerShell 1.0, you would have needed to use Remote Desktop to run an interactive session on a remote server.

1000 things 1% better!