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.
This is certainly not the most efficient way, but you can take a look at the results of
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.
This is based on the code from Oscar Beh.
Here we report all instances of
If you see two Session ID's reported, run the script again.
or, in my case (using “Switch user”)
(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
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
It turns out that
To use this in a script (batch file), you might want to say
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?