fredag 10 juni 2011

PowerShell: Fetching the current user

To get the name of the user running the script, use: [Security.Principal.WindowsIdentity]::GetCurrent().Name

You could also use $Env:UserName and $Env:UserDomain, but these are not always what you might think at a first glance. They contain the name of the user in the active environment, which is not always the same as the user running the script.

Example (assume that the user running this line is 'foo'):

runas /user:bar /env "powershell myscript.ps1" This would run the script as 'bar' specified, but with $Env:UserName set to 'foo' because of the parameter /env

Inga kommentarer:

Skicka en kommentar