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?
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().
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 ( . ).
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.
You should be able to use:
(Get-Process -PID $pid).SessionID
You can
PS C:\Users\AlexK> [System.Diagnostics.Process]::GetCurrentProcess().SessionId
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With