Getting all open PS Sessions on a remote server (from new console window) Getting all open PS Sessions on a remote server (from new console window) powershell powershell

Getting all open PS Sessions on a remote server (from new console window)


From what I can tell...

The PSSessions you created live on "MyServerName" and, for the duration of the session you first created them, will also be returned by Get-PSSession (with no arguments, in the PowerShell window on the box you're remoting FROM). When you close the session they are created in, the sessions are no longer on your computer. This is why Get-PSSession doesn't return anything when you close and open a new PowerShell window. They never "lived" on your computer, they are remote sessions, however, they were in scope in your original PowerShell window because that is the local scope you created them in.

If your sessions are still on MyServerName, as it seems they are due to the error you mention about max sessions, then typing the following command should list them:

Get-PSSession -ComputerName MyServerName

If you wanted to reconnect them all in your existing session/window, you could do:

Get-PSSession -ComputerName MyServerName | Connect-PSSession

To remove them all, enabling you to create new PSSessions to MyServerName

Get-PSSession -ComputerName MyServerName | Remove-PSSession

Looking a bit further in the docs, all sessions do not live indefinitely when you close your PowerShell window. See:

Get-Help about_Remote_Disconnected_Sessions -ShowWindow

partial excerpt (with emphasis mine):

If you close (exit) the session in which a PSSession was created while commands are running in the PSSession, Windows PowerShell maintains the PSSession in the Disconnected state on the remote computer. If you close (exit) the session in which a PSSession was created, but no commands are running in the PSSession, Windows PowerShell does not attempt to maintain the PSSession.

From what I can see, sessions that aren't a) disconnected, or b) busy running a command, are discarded when you close the PowerShell window you started the PSSessions from. Additionally, the documentation does seem to mention there are also timeouts (which probably depend on PSSessionConfigurations on the server, but I don't know anything about those yet myself (other than they exist).

This was a good excuse for me to sift through some of the PowerShell Remoting documentation, also look at:

Get-Help *PSSession*Get-Help *remote*


From my experimentation, if the sessions aren't doing anything then they get closed on the remote end. To prevent that, either have them do something e.g.:

Invoke-Command -Session $s { ... } -AsJobInvoke-Command server01 { ... } -Disconnected

Or the other option is to disconnect your sessions:

Disconnect-PSSession -Id (1..5)

Both approaches will result in the remote sessions staying alive.


Having this same issue today I came across this nice set of functions from jrich.I even simply pasted the functions into my PS window and ran

"<computername>" | Get-RemotePSSession | Remove-RemotePSSession

and voila! no more left-open sessions on said computer.

Here's the direct link to his blog.https://jrich523.wordpress.com/2012/01/19/managing-remote-wsman-sessions-with-powershell/#comment-1079