How can I retrieve the Session ID from command line?
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
This combines aspects of Gary’s answer and Oscar’s answer.
As noted by Gary, the output of query session
looks something like
SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
>console janedoe 1 Active
rdp-tcp 65536 Listen
or, in my case (using “Switch user”)
SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
>console gman 1 Active
otherguy 2 Disc
(I don’t have RDP set up, so I can’t test with it.)
Presumably the intent of the question is to determine
the ID of the currently active session.
I suggest searching the output of query session
for what looks like the current session:
for /f "tokens=2-4" %a in ('query session') do @if "%a"=="%username%" if "%c"=="Active" echo %b
which assigns the 2nd, 3rd, and 4th word
(which we hope to be the USERNAME, [SESSION] ID, and STATE)
from each line of the output of query session
to %a
, %b
, and%c
,
respectively, and then reports SESSION ID from the line
where USERNAME is me and STATE is Active
.
It turns out that query session
takes a username
parameter,
so the above can be simplified to
for /f "tokens=2-4" %a in ('query session %username%') do @if "%c"=="Active" echo %b
or
for /f "tokens=3-4" %a in ('query session %username%') do @if "%b"=="Active" echo %a
To use this in a script (batch file), you might want to say
set MY_SESSION_ID=unknown
for /f "tokens=3-4" %%a in ('query session %username%') do @if "%%b"=="Active" set MY_SESSION_ID=%%a
using %%a
and %%b
(instead of %a
and %b
) because it’s in a script.
You can probably leave out the @
if the entire batch file is under @echo off
.
The initialization to unknown
lets you do error-handling
(detecting if there is no line in the output of query session
that matches).
Extending the error-handling to recognize and react
if there are multiple matching lines in the output of query session
is left as an exercise.
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%
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
This is based on the code from Oscar Beh.
tasklist
will show report the session ID (and other information) for all instances of a given executable, including itself. As you're running the command, it will show your session id. The only potential defect in his code is that it does not inform you if there are multiple instances of the program, which can cause the script to return incorrect results.
Here we report all instances of tasklist
to ensure that there is not another session running that command:
for /f "tokens=4 delims= " %%G in ('tasklist /FI "IMAGENAME eq tasklist.exe" /NH') do echo Session ID %%G
If you see two Session ID's reported, run the script again.