#PSTip Managing Remote Sessions

Note: This tip requires Windows PowerShell 2.0 or above.

The Get-WSManInstance cmdlet can be used to view the sessions that are connected to a remote computer. You can run this cmdlet from any client that’s running PowerShell 2.0 or higher. Getting the number of WinRM sessions by user provides interesting information, especially when you are troubleshooting fan-in scenarios.

PS> Get-WSManInstance -ConnectionURI http://myserver.mydomain.com:5985/wsman shell -Enumerate

rsp             : http://schemas.microsoft.com/wbem/wsman/1/windows/shell
lang            : en-US
ShellId         : 75A89E0E-E6E5-477D-AD0F-DAD6706CC236
ResourceUri     : http://schemas.microsoft.com/powershell
Owner           : Mydomain\user1
ClientIP        : 192.168.2.11
ProcessId       : 5800
IdleTimeOut     : PT180.000S
InputStreams    : stdin pr
OutputStreams   : stdout
BufferMode      : Block
State           : Connected
ShellRunTime    : P0DT0H17M7S
ShellInactivity : P0DT0H0M7S
MemoryUsed      : 134MB
ChildProcesses  : 0

rsp             : http://schemas.microsoft.com/wbem/wsman/1/windows/shell
lang            : en-US
ShellId         : C334FE90-8CA7-4F26-8516-084EF68A2F32
ResourceUri     : http://schemas.microsoft.com/powershell
Owner           : 92.168.2.11
ClientIP        : ***
ProcessId       : 9592
IdleTimeOut     : PT180.000S
InputStreams    : stdin pr
OutputStreams   : stdout
BufferMode      : Block
State           : Connected
ShellRunTime    : P0DT0H22M25S
ShellInactivity : P0DT0H0M24S
MemoryUsed      : 91MB
ChildProcesses  : 0
(...)

Not only can you see the remote connections, but you can also “kill” connections by using Remove-WSManInstance. In this example, I’m using the command to kill the second session:

PS> Remove-WSManInstance -ConnectionURI http://myserver.mydomain.com:5985/wsman shell @{ShellID="C334FE90-8CA7-4F26-8516-084EF68A2F32"}

If you want to remove all of the sessions, you can restart the WinRM service, as shown in the next example:

PS> Restart-Service -Name WinRM

You can find more information about managing remote sessions HERE.

Share on: