Powershell - Find the user who invoked the script Powershell - Find the user who invoked the script powershell powershell

Powershell - Find the user who invoked the script


Interesting question. I wrote a script with three different ways to get the user like so:

([Environment]::UserDomainName + "\" + [Environment]::UserName) | out-file test.txt"$env:userdomain\$env:username" | out-file -append test.txt[Security.Principal.WindowsIdentity]::GetCurrent().Name | out-file -append test.txtnotepad test.txt

Saved it as test.ps1 and called it using runas as:

runas /user:domain\user "powershell e:\test.ps1"

And I got the domain\user all three times in the output. Used runas to just distinguish between the user I am logged in as (me!!) and the domain\user with which I was running it as. So it does give the user that is running the script.