Determine if the Terminal Server console session is in use, from a command line

A couple of times lately I have been asked how to determine form a command line, if the console session is currently in use on a Windows 2003 server. One option is as as follows:


 From a connected TS/RDP session you can run set sessionname This will return “console” or “RDP-Tcp#X” where X is the session numberHowever it only shows “console” if the user is at the physical console, not if they have remotely connected to the console session using mstsc /console  A better option is to use the query command:  query session This will return a list similar to:

SESSIONNAME   USERNAME    ID     STATE     TYPE          DEVICE
rdp-tcp#7             Bob            0      Active    rdpwd
rdp-tcp                            65536      Listen   rdpwd
rdp-tcp#2             Sue            1      Active    rdpwd
rdp-tcp#2             Tom           2      Active    rdpwd
console                                   4      Conn     wdcon

In this list, “Conn” indicates someone is remotely connected to the console session, and ID “0” is the user using the console session.


If Bob were at the physical console it would look like the following: 


SESSIONNAME   USERNAME    ID     STATE     TYPE          DEVICE


console                 Bob            0      Active   wdcon
rdp-tcp                            65536      Listen   rdpwd
rdp-tcp#2             Sue            1      Active    rdpwd
rdp-tcp#2             Tom           2      Active    rdpwd
 Note: the query command only returns the console session information if run as an admin. Other session information is available to users.

Should you want to run the command on a remote server, you can use Sysinternal’s/Microsoft’s free PSexec command line utility. If logged in as a domain admin the basic syntax would be: PSexec  \ServerName  query  session Detailed use and syntax of PSexec can be found:


http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx