Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current Windows session ID in PowerShell

Tags:

powershell

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?

like image 456
s4nk Avatar asked May 07 '15 13:05

s4nk


People also ask

How do I find my current session ID?

Before getting a session id you need to start a session and that is done by using: session_start() function. Now that you have started a session you can get a session id by using: session_id().

How do I see active sessions in PowerShell?

To get the sessions on the specified computers, PowerShell creates a temporary connection to each computer and runs a Get-PSSession command. Type the NetBIOS name, an IP address, or a fully-qualified domain name of one or more computers. To specify the local computer, type the computer name, localhost , or a dot ( . ).

How do I find the login ID in PowerShell?

To get current username in PowerShell, use whoami, GetCurrent() method of WindowsIdentity . Net class or Get-WMIObject cmdlet in PowerShell. In PowerShell, there are different ways to get current user name.


2 Answers

You should be able to use:

(Get-Process -PID $pid).SessionID
like image 125
EBGreen Avatar answered Sep 28 '22 09:09

EBGreen


You can

PS C:\Users\AlexK> [System.Diagnostics.Process]::GetCurrentProcess().SessionId
1
like image 43
Alex K. Avatar answered Sep 28 '22 09:09

Alex K.