Dan Rigsby – Coding Up Style

Developer.Speaker.Blogger

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:

RemoteDesktopExceeed

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.

cmd3

You can also use quser.exe:

quser /server:<serverName>
 
Here is the same example above but notice it also when they logged in and how long they were idle.

cmd1

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.

cmd2

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.

kick it on DotNetKicks.com

15 Responses to “Remotely Log Off Remote Desktop Users”

  1. Greg Finzer Says:

    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

  2. Jay R. Wren Says:

    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.

  3. Peter Kellner Says:

    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.

  4. David Kemp Says:

    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)

  5. Dan Rigsby Says:

    I dont think quser is in XP, but qwinsta and logoff should be.

  6. Weekly Link Post 57 « Rhonda Tipton’s WebLog Says:

    [...] Dan Rigsby shows us how to Remotely Log Off Remote Desktop Users. [...]

  7. Remotely Log Off Remote Desktop Users | Samuel Kim Says:

    [...] From Dan Rigsby’s blog post. [...]

  8. Mark Riordan Says:

    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

  9. Akshay Says:

    This commands working fine with vista also.

  10. Hein Says:

    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

  11. Tom Courtney Says:

    Excellent explaination, the only problem that i have found is that quser has been removed from XP Pro SP2

  12. Jim Says:

    @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.

  13. Erwin Says:

    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…

  14. stew Says:

    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)

  15. Sumoy Says:

    Many many thanks Dan! this article serves my need

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>