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
%%G would be if you call it from a batch, %G if directly from the command line –  Micah Shappard May 18 at 16:05
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  G-Man May 18 at 18:20

3 Answers 3

up vote 7 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".


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


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
Right, from the command line, type tasklist. –  Gary Jun 29 '11 at 14:30
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

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
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
Oscar's solution actually worked for me, when replaced %%G with %G: for /f "tokens=4 delims= " %G in ('tasklist /FI "IMAGENAME eq tasklist.exe" /NH') do SET RDP_SESSION=%G –  Paweł Stankowski Jan 15 at 10:53

protected by Community May 18 at 19:06

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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