Wednesday, October 20, 2021

Query the Signed in User When Running Script as System

I'm working on a desktop migration project where we run some PowerShell scripts to prepare the computer for migration. As part of this we need the locally signed in user.

Normally, you can obtain locally signed in user from an environment variable:


However, we're running the script as SYSTEM. So, that returned value is incorrect. That value is the username associated with the PowerShell instance.

You can query the signed in user when you run a script as SYSTEM by using Get-WmiObject:

(Get-WmiObject -ClassName Win32_ComputerSystem).Username

No comments:

Post a Comment