I was looking for a way to display the session-ID of my current terminal server session regardless of the protocol I’m using in a batch file. I tried numerous workarounds by parsing the output of qwinsta.exe or quser.exe – but both ways didn’t work in a reliable manner.
After some research, I figured out that the session ID is available as a property within the .NET Framework. Therefore, I decided to write a small executable that will output the session id.
Attached to this post you’ll find the SESSIONID.EXE – feel free to use it within your project.
Here are some code snippets how to use the sessionid.exe:
FOR /F "tokens=*" %%A IN ('sessionid.exe') DO SET SESSIONID=%%A ECHO %SESSIONID%