Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I retrieve the current user session ID via windows command line?

In the tasklist under the users tab, each user is associated with a session ID. I want to retrieve this via command line so I can use it for a remote application.

share|improve this question
    
I'm not sure what you mean by session ID? –  EBGreen Jun 29 '11 at 14:19
    
In the tasklist under the users tab, each user is associated with a session ID. I want to retrieve this via command line so I can use it for a remote application. –  Negative Jun 29 '11 at 14:30
    
also see superuser.com/questions/123242/… –  matt wilkie Dec 20 '12 at 18:45

3 Answers 3

up vote 4 down vote accepted

This is certainly not the most efficient way, but you can take a look at the results of tasklist. It will display the name and session# of each process running. If you are logged in locally then look at the ID for the session named "console".

d:\>tasklist

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
System Idle Process              0 Services                   0         24 K
System                           4 Services                   0      8,580 K
smss.exe                       316 Services                   0      1,500 K
...snip

Edit:

Query Session will retrieve the session id:

d:\>query session
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
>console           janedoe                   1  Active
 rdp-tcp                                 65536  Listen
share|improve this answer
    
I'm looking to retrieve it via command line. –  Negative Jun 29 '11 at 14:29
1  
Right, from the command line, type tasklist. –  Gary Jun 29 '11 at 14:30
1  
Hmm actually you might be on to something here. edit: tasklist /FI "imagename eq cmd.exe" /FO list I plan on parsing this output. Thanks! –  Negative Jun 29 '11 at 14:35
    
Or I can do that. Nice –  Negative Jun 29 '11 at 14:36
for /f "tokens=4 delims= " %%G in ('tasklist /FI "IMAGENAME eq tasklist.exe" /NH') do SET RDP_SESSION=%%G
echo Current RDP Session ID: %RDP_SESSION%
share|improve this answer

Unlike the Session Name, the Session ID is not stored in a variable and is not as easy to get to. I am sure there is one, but I can't think of a built in command line tool that does this.

However, I have written a tiny .exe file (C#, .Net 4) that does this.

It will both display the session id, and also have it as the return code so that you can use this in a variety of scripts/techniques.

Link to file on MediaFire

share|improve this answer
    
@Kcotreau - i.imgur.com/5mUFJ.png –  William Hilsum Jun 29 '11 at 14:42
    
I realized. :). –  KCotreau Jun 29 '11 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.