10

Every Win32_Process object contains a SessionId field. What is the simplest way to get the current Windows session ID in PowerShell? By current I mean the session in which the script is running. I'd like to avoid parsing the output of the query session command. If that's not achievable, is it possible to list all processes in the current session?

s4nk
  • 647
  • 1
  • 9
  • 18
  • I would also be curious but if you get stuck you can use my answer [here](http://stackoverflow.com/a/29130697/3829407) to parse the data from `qwinsta` into a PowerShell object. – Matt May 07 '15 at 13:43

2 Answers2

14

You should be able to use:

(Get-Process -PID $pid).SessionID
EBGreen
  • 36,735
  • 12
  • 65
  • 85
8

You can

PS C:\Users\AlexK> [System.Diagnostics.Process]::GetCurrentProcess().SessionId
1
Alex K.
  • 171,639
  • 30
  • 264
  • 288