If you use Windows Terminal Services for remote desktop access, it’s common to run into this nasty error message from time to time:
The terminal server has exceeded the maximum number of allowed connections.
The error refers to the maximum 2 remote connections that are allowed to a Windows 2000 server (or up… XP only allows 1). Now, only a crazy person would ever consider connecting more than one or two users remotely to a server, and even though that would never, ever, even in a million years, need to happen, that error still crops up from time to time.
The problem is that if a user closes their terminal services client without logging out, their remote session switches to “disconnected” mode and will continue using up one of those two available connections.
While I’m sure we all agree that this is an awesome feature, sometimes in an emergency you need to log into a server and all of its connections are already in use. There are a couple of really useful and mostly unknown command line utilities that will allow you to remotely find and kill other remote desktop sessions, whether they are in a connected or disconnected state.
On most systems, these tools are qwinsta.exe and rwinsta.exe, though on some 2003 systems these appear to have been renamed and might be available as query.exe and reset.exe, respectively. Use whichever is available on your machine.
Finding disconnected sessions with qwinsta or query
Run “qwinsta /server:theserver_or_ip” or “query session /server:theserver_or_ip”, using the server name or ip address of the machine you can’t log in to. You’ll get something like the following:
SESSIONNAME USERNAME ID STATE TYPE DEVICE console 0 Conn wdcon rdp-tcp 65536 Listen rdpwd rdp-tcp#5 auser 1 Active rdpwd rdp-tcp#7 another 2 Disc rdpwd
From this, you can see that users “auser” and “another” are using the two remote Terminal Services connections. Furthermore, the remote desktop client that “auser” is using is still connected to the server, whereas “another” is in a disconnected state, probably the result of closing the remote desktop window instead of logging off.
Killing disconnected sessions with rwinsta or reset
Run “rwinsta sessionid /server:theserver_or_ip” or “reset session sessionid /server:theserver_or_ip”, using the session id obtained above (1 for “auser” and 2 for “another”) and the name or ip address of the server. In this case, you’d probably want to kill session 2, since that user is disconnected anyway.
The command will run without a response, but if you run qwinsta (or query) again, you’ll notice that the session is no longer there. More importantly, if you try and connect to the machine using remote desktop, you’ll get in.