Disconnect inactive RDP sessions

While I’m working in a project that needs to log in into a server I usually leave my user session opened (yes I know that it s not a good practice, but I always forget to log off).

So when an admin needs to connect to the server there are either no free sessions or probably the openend sessions are consuming resources that could be slowing down the server.

So as a power user, I discovered the following command to execute in PowerShell (remember to execute it as administrator) to try to list and free the unused sessions:

If you only want to see the sessions, you can execute only this:

query user | select -Skip 1 | ? {($_ -split “\s+”)[-4] -eq ‘Disc’}

To disconnect all these sessions at once, you can execute this command inside an elevated PowerShell window:

query user | select -Skip 1 | ? {($_ -split “\s+”)[-4] -eq ‘Disc’} | % {logoff ($_ -split “\s+”)[-5] /v}

And then all disconnected RDP sessions are forced logoff and the resources are free.

Till next time!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s