Killing Disconnected Terminal Server Sessions from the Command Line

By | 2013-08-26

Terminal Services (or Remote Desktop if you prefer) is a good thing. It allows you to remotely connect to a server to manage it.

However, there is a “special” breed of user out there that can’t remember to log off of their sessions, opting to simply click the “X” in the upper right. Too many disconnected sessions (2), and nobody else can connect via RDP. Kill ’em quick with this process.

First, why are you using terminal services to manage your server? You should be using remote administration tools, usually via an MMC interface. All core services (AD, DHCP, DNS, etc), as well as most application services (Exchange, IIS, etc) can be administered with it. Heck, you can even use PowerShell to administer remote servers. So stop using Terminal Services to manage your servers.

Second, if you absolutely must RDP into a server, sign off correctly. Windows servers have a maximum of two RDP sessions and one console session available, unless the server has been licensed for use as a terminal server. Leaving two disconnected RDP sessions on the server effectively blocks anyone else from connecting to the server via RDP.

Normally, to clear the disconnected sessions, one would fire up the terminal services manager MMC (remote management!), connect to the server in question, and log off any disconnected sessions. It doesn’t take long to do this, but can be a few minutes if you are working with a server that is on the other side of the country.

So why not try the command prompt? Windows (at least from Win7 onward, as well as server OSs) have a number of utilities built-in to manage Terminal Services remotely. The two we are interested in are:

  • Query Session
  • LogOff

The first thing you’ll need to do is open a command prompt with credentials that have permissions to administer terminal services on the remote server. If you don’t do this, you’ll simply get “Access Denied” errors when attempting to administer the remote server.

Once you get the command prompt open, type the following command:

query session /server:MyTestServer

The output of the command will be something similar to the following:

C:\>query session /server:MyTestServer
SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
services                                    0  Disc
console                                     1  Conn
                  MyTestUser                2  Disc
rdp-tcp                                 65536  Listen

C:\>

Notice the session with MyTestUser, and its current state of Disc. This means that the user did not log off the machine, but rather the connection was either disrupted, or the user simply closed the RDP window without logging off.

Just an FYI, the full syntax of the Query Session command is:

query session [{SessionName | UserName | SessionID}] [/server:ServerName] [/mode] [/flow] [/connect] [/counter]

Parameters

SessionNameThe name of the session you want to query.
UserNameThe name of the user whose sessions you want to query.
SessionIDThe ID of the session you want to query.
/server:ServerNameIdentifies the terminal server to query. The default is the current server.
/modeDisplays current line settings.
/flowDisplays current flow-control settings.
/connectDisplays current connect settings.
/counterDisplays current counters information, including the total number of sessions created, disconnected, and reconnected.
/?Displays help at the command prompt.

So, now that you have a list of the sessions, including the disconnected session, you have what you need to kick that “user” off the dead RDP connection. Make note of the ID number of the disconnected session, in this case 2.

Now, at your handy-dandy super-elevated command prompt, type the following:

Logoff 2 /server:MyTestServer

One thing you’ll notice immediately is that you’ll get no feedback if the command was successful. You’ll simply get returned to the command prompt. You can verify the connection was disconnected by running the Query command again. The whole session would look like this:

C:\>logoff 2 /server:MyTestServer

C:\>query session /server:MyTestServer
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
 console                                     1  Conn
 rdp-tcp                                 65536  Listen
 
C:\>

Notice the previously noted dead session, which had an ID of 2, is now missing from the list. Congratulations! You have now cleared out the blocking session.

Questions? Comments? Fire off in the comments below.

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.