Remotely Log Off Remote Desktop Users
Posted by Dan Rigsby on August 26th, 2008
I frequently use Remote Desktop to access various PCs here at work and nothing annoys me more than seeing this message box:
This is caused when “The terminal server has exceeded the maximum number of allowed connections.”, right? Basically, this happens because Windows by default only allows two simultaneous terminal services connections to the same machine. If you see this message, then there is already that number of people logged in, and you cannot connect until one of the sessions logs off.
Usually what happens is that people don’t explicitly log out of machines when they disconnect from remote desktop which causes their “rogue” sessions to remain active. You could just nicely ask everyone if they would please “log off” before disconnecting their sessions, but is there something else we can do?
To perform the commands I am about to show you, you need to be an administrator on the target machine. If you aren’t you can’t perform these commands. However, in most development and test environments, hopefully this won’t be an issue.
How to query for users on a machine
First, how can we query to find out what users have a session on a remote machine? Windows provides the qwinsta.exe command which we can use to query for the sessions that are running The format is as follows:
qwinsta /server:<serverName>
Here is an example running this command against one of my local machines. Notice it shows the username, state, and the ID of the session.
You can also use quser.exe:
quser /server:<serverName>
How to log a user off of a machine
Now that we know what users are on a machine, how can we force one to disconnect? Again, there is a handy little command called logoff.exe that we can use to force a user to log off of a machine based off of their session ID. The format is as follows:
logoff <sessionId> /server:<serverName>
Here is an example running this command against one of my local machines. Notice that I used the session ID that I found from the quser.exe command above.
Warning: If you remotely log off a user, their log session goes away which could mean that the unsaved data is lost, or if the user is in the middle of an activity, they may come down to your office to chew you out.

















August 26th, 2008 at 8:35 am
Dan,
I have found that you can do an MSTSC at the command line before doing a remote desktop connection and that will also let you in.
Thanks,
Greg
Free Cheat Sheets for C#, NUnit, CSS, Regular Expressions and more.
Quick Reference Pack
August 26th, 2008 at 2:29 pm
I’m not sure if this is what @Greg is talking about above, but you have 1 extra session, the “console” session if your server is in remote admin mode.
You have to specify the /console switch when running mstsc. I connect like this:
mstsc /console /v:myserver
It really is the console. If someone is at the console, they should see what you are doing. Sound and Disks don’t get mapped through mstsc IIRC. But this can get you into the server so you can use the local server tools to kick out the other sessions.
Of course as a lover of command line tools, I prefer your solution, but for the command line fearing, the terminal server /console might be an easier option.
August 26th, 2008 at 5:28 pm
I have used Jay R. Wren’s solution before because I thougth I was the only one that knew about it. Now that everyone knows, there will likely be no logins left after the console one is used up so.. Thanks Dan!, I will be using your solution for now on. Great post.
August 27th, 2008 at 8:37 am
These commands don’t exist in XP – are they windows server only, or do they exist in Vista?
(I can use psexec to get to them anyway, but it would be good to know)
August 27th, 2008 at 9:01 am
I dont think quser is in XP, but qwinsta and logoff should be.
September 1st, 2008 at 2:48 pm
[...] Dan Rigsby shows us how to Remotely Log Off Remote Desktop Users. [...]
October 8th, 2008 at 6:22 am
[...] From Dan Rigsby’s blog post. [...]
November 7th, 2008 at 5:54 pm
This works great! You don’t need /server if you run it on the local machine. Here’s what I got. Hopefully the formatting will come out OK:
C:\>qwinsta
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console 0 Conn wdcon
rdp-tcp 65536 Listen rdpwd
administrator 1 Disc rdpwd
>rdp-tcp#3 Administrator 2 Active rdpwd
C:\> logoff 1
January 3rd, 2009 at 6:24 am
This commands working fine with vista also.
March 6th, 2009 at 1:51 pm
Hello, thanks for your clear explanation. But…This only helps when you have physical access to the target machine. What about if this is too far away? so my question: can we also logoff sessions without a physical presence on the target machine (the reason why you use rdp in the first place..)
thanks, Hein
May 4th, 2009 at 7:15 pm
Excellent explaination, the only problem that i have found is that quser has been removed from XP Pro SP2
May 7th, 2009 at 7:47 am
@Hein: It must not have too clear to you. These commands, when typed at a workstation, tell you who’s logged on to a Remote Server and allows you to log them off — Remotely.
June 19th, 2009 at 6:37 am
But what if youre logged in as a user/admin in a different domain?
I mean: I’m logging in on my notebook as Erwin@BlaBla.com and i want to log-off users on Server01.Hubba.lan? remotely…
February 1st, 2010 at 12:48 am
Nice, I’d wondered about that, but I usually just connect to the console session and starting logging people off … mstsc /admin (or /console depending on the version)
February 10th, 2010 at 1:20 am
Many many thanks Dan! this article serves my need