How to logoff remote desktop sessions via command line tools?

This trick I learned from my one of ex-college.  In Windows servers, only two remote desktop sessions allowed with administrative access. There are situations, you can’t connect to the server via remote desktop because of two sessions already active.  You may have seen this dialog box. ("Remote Desktop Disconnected", "This computer can’t connect to the remote computer")

Usually Windows Administrators, open "Terminal Services Manager" (in Administrative tools), connect to the problematic server and log off the unwanted sessions.  There are easy ways if you love command line based tools.

Query the Remote Server for Current Terminal Sessions

To query and list the sessions on the remote session, you could use QUser.exe or QWinsta

1. QUser

QUser command comes with all the latest Windows clients and servers. This will list the sessions of the remote server (or local machine).

NOTE: if you are using Windows XP, you need to add this location into your System path: C:WindowsSystem32DLLCache. To do this, in command prompt, type the following.

SET PATH = %PATH%;C:WindowsSystem32DLLCache;

QUser help shows,

C:Anand>QUser /?
Display information about users logged on to the system.

QUERY USER [username | sessionname | sessionid] [/SERVER:servername]

  username            Identifies the username.
  sessionname         Identifies the session named sessionname.
  sessionid           Identifies the session with ID sessionid.
  /SERVER:servername  The server to be queried (default is current).


C:>quser /server:MyCitrixSVR
johndoe               ica-tcp#966        10  Active          7  7/31/2008 3:04 PM
averagejoe            ica-tcp#969         1  Active          9  7/31/2008 3:30 PM
familyman             ica-tcp#984         5  Active       1:06  7/31/2008 4:33 PM
normaldude            ica-tcp#987         2  Active          4  7/31/2008 6:20 PM

2. QWinsta

QWinsta is little different and better. It has more features and options. It comes with all flavors of Windows.  QWinsta command line help displays as,

C:>qwinsta /?
Display information about Terminal Sessions.

QUERY SESSION [sessionname | username | sessionid]
              [/SERVER:servername] [/MODE] [/FLOW] [/CONNECT] [/COUNTER]

  sessionname         Identifies the session named sessionname.
  username            Identifies the session with user username.
  sessionid           Identifies the session with ID sessionid.
  /SERVER:servername  The server to be queried (default is current).
  /MODE               Display current line settings.
  /FLOW               Display current flow control settings.
  /CONNECT            Display current connect settings.
  /COUNTER            Display current Terminal Services counters information.



C:>qwinsta /server:citrixserver
SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
                                             0  Disc    rdpwd
ica-tcp                                 65536  Listen  wdica
rdp-tcp                                 65537  Listen  rdpwd
console                                    16  Conn    wdcon
ica-tcp#966       johndoe                  10  Active  wdica
ica-tcp#969       apple                     1  Active  wdica
ica-tcp#984       averagejoe                5  Active  wdica
ica-tcp#987       familyman                 2  Active  wdica
ica-tcp#989       whoisme                   3  Active  wdica

You are welcome to try the other options QWinsta provides.

Logoff the Remote Sessions

To Log off the terminal session of the remote server, you can use any one of two command line tools. One of LOGOFF and another one is RWINSTA. Before you log off the remote session, you should know the "Session ID" which you get it from "QUSER" OR "QWINSTA" commands as above stated.

1. Logoff

Logoff command kicks off (logging off) the specified remote session. Log off help shows,

C:>logoff /?
Terminates a session.

LOGOFF [sessionname | sessionid] [/SERVER:servername] [/V]

  sessionname         The name of the session.
  sessionid           The ID of the session.
  /SERVER:servername  Specifies the Terminal server containing the user
                      session to log off (default is current).
  /V                  Displays information about the actions performed.


C:>logoff /server:infra-apps 1 /v
Logging off session ID 1

2. RWinsta

RWinsta has same parameters and does same thing as log off command. It simply means Reset WINdows STAtion. The help goes as,

C:>RWinsta /?
Reset the session subsytem hardware and software to known initial values.

RESET SESSION {sessionname | sessionid} [/SERVER:servername] [/V]

  sessionname         Identifies the session with name sessionname.
  sessionid           Identifies the session with ID sessionid.
  /SERVER:servername  The server containing the session (default is current).
  /V                  Display additional information.


C:>RWinsta /Server:MyWinServer 1

Note: RWinsta command does not return anything.

Happy Re-setting the remote sessions (Hey, stop cursing your co-workers who didn’t log off their sessions).

Keywords: Reset remote session, Remote Desktop Session


44 comments on “How to logoff remote desktop sessions via command line tools?

  1. Palani says:

    This knowledge sharing helped me a lot in cirtical situation , thanks a ton 🙂

  2. P G says:

    Good post.I guess the above commands would logoff and kill existing processes and a login will give you a fresh instance.  Is there a way similar to Task manager -> Users -> Disconnect where you can disconnect / take control of an existing remote desktop session without killing the programs currently executing?  Note: I’m doing this over the internet, so don’t have remote MMC access.  Just have an SSH (command line) session with the target server.

  3. Spoke says:

    PSTools has this as well:
    psloggedon \\[ip or netname]
    psshutdown -o for logoff console user use -f to force apps to close

  4. Anand says:

    I never checked PSTools for this. But it’s very good know. I always used "shutdown" command  to shutdown or reboot which comes with XP and up OSes.

  5. Andrew says:

    Very good!

  6. Mahesh says:

    I am unable to find QUser.exe or QWinsta command in windows XP, please let me know the path…

    • I hope you are not using XP Home edition. Open your command prompt and type this command at C:\ in command prompt: Dir Qwinsta*.* /s

      This command will search the while C: dirve and tell you where QWinsta is. I am sorry I don’t have any XP computers nearby to check it for you.

  7. Flavio says:

    windows 7 returned error

    Erro 0x00000005 ao enumerar sessionnames
    Erro [5]:Acesso negado.

    • Open your command prompt in elevated mode (run as administrator) and try it. Remember you have to be administrator for the target server.

    • Sajir says:

      Open a command Promt by selecting Run As different user.
      Then, enter user name “remotehostname\administrator” and password.

    • don says:

      the AllowRemoteRPC key has a value of 0 – which is why it is failing. To fix this, run this command.

      START > Run > cmd >

      Reg ADD “HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server” /v AllowRemoteRPC /t REG_DWORD /d 1 /f

  8. david says:

    Good info thanks. Only one issue in that your path up the top of the page says SET PATH = %PATH%;C:WindowsSystem32DLLCache; when it seems it should include the backslashes and actually be SET PATH = %PATH%;C:\Windows\System32\DLLCache;

    Thanks David

  9. […] How To Logoff Remote Desktop Sessions via Command Line Tools Share this:TwitterFacebookLike this:LikeBe the first to like this post. […]

  10. Also, it would be good to specify that this is for SERVERS ONLY! Not for workstations – by that, I mean you cannot use this to “list sessions running on a workstation.”

    This ONLY works to list sessions running on SERVERS!
    You can run the tool FROM your worsktation, but you CANNOT list sessions of another workstation – it would be a nice feature, though. There are times when various tools have taken control, via ‘console session’ or other session, on workstations, and we need to know WHO owns the session; be able to kill the session, etc.

  11. Terry Lau says:

    Thanks for sharing. It is useful.

  12. Betty says:

    Outstanding quest there. What happened after? Take care!

  13. Ssingh says:

    For Could not logoff session ID 6, Error code 5
    Error [5]:Access is denied.

    Try this… On Task Manager, Go to processes and click on “show processes from all users” . Once you have done that, go back to user’s tab and right click on the session and click on “LogOff”. That should do the trick :d

  14. Argiris D. says:

    Thanks a lot

  15. Bulbous says:

    This was working for me in Server 2003, but now in Server 2008, I am getting “Access is Denied”. UAC is disabled.

  16. Bobbie says:

    Wonderful post! We are linking to this particularly great article on our site.
    Keep up the good writing.

  17. bofa com says:

    Thanks for sharing your thoughts. I really appreciate your efforts and
    I will be waiting for your next post thanks once again.

  18. Wasco says:

    Great post very helpful!

  19. Ravi Chopra says:

    Thanks for this great information to logoff terminal session. However, it is also possible via command line tool.. Please check out

  20. Ali Salim says:

    Thanks , wonderful topic , really helpful

  21. Radek says:

    Very usefull and straightforward info. Thank you!

  22. BARI says:

    thanks for help full full commed

  23. Dark says:

    Very helpful – just what i was looking for – THANK YOU

  24. Balu says:

    How to log off all active sessions except current one? I need to run it daily I cant mention session ID every time. is it possible ?

  25. says:

    Realy nice. This helps me a lot.


  26. Mridul says:

    Good post! Thanks for the share.

  27. wtuser says:

    Thank You very much.. It works on Windows 2003 Server.

  28. Laszlo says:

    WIndows 2003 Server (SP 2) Standard Edition. It is not working with user stuck on console session (ID 0).

    C:\>rwinsta 0 /v
    Resetting session ID 0
    Session ID 0 has been reset

    But the user remain stuck.

    ************ 0 Disc rdpwd

    We need to get rid of this session without restarting the server.

    Any suggestions?

  29. Sandeep says:

    This knowledge sharing helped me a lot in critical situation , thanks a ton.

  30. Guillermo says:

    This knowledge sharing helped me a lot in a non-critical (but very annoying) situation, thanks a ton from Mexico!

  31. Srinivasan Bhaskharan says:

    Thanks – valuable tip

  32. Kevin Moncrieffe says:

    This helped me today. Thank you

  33. Bridec says:

    […] August 1, 2008 –Anand– […]

  34. […] How to logoff remote desktop sessions via command line tools? […]

  35. THANK YOU!!! Qwinsta = Best. Command. Ever.

  36. manoj says:

    Hi Dude,

    how to logoff list of users at a time in windows server, is there any command. please help me

Leave a Reply

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

You are commenting using your 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