In many cases, potentially with scripts triggered by scheduled tasks you need to query the User Principal Name (UPN) or the actual user who is currently logged in or running a task.
The command that you are looking for is:
whoami /upn
Example
PS C:\> whoami /upn alice@protectigate.com PS C:\>
Alternatives to get Usernames and Domains
These following command will query the environmental variables in the system.
I. PowerShell
# Get user's SamAccountName PS C:\> $env:USERNAME alice # Get the NETBIOS name of the userdomain PS C:\> $env:USERDOMAIN PRO # Get the DNS name of the domain PS C:\> $env:USERDNSDOMAIN PROTECTIGATE.COM # Combine them to get the UPN. Only works if no alternative domain suffixes are used in the domain PS C:\> "$env:USERNAME@$env:USERDNSDOMAIN" alice@PROTECTIGATE.COM PS C:\>
II. Cmd line
# Same as before only using the cmd variant of the environmental variables C:\>echo %USERNAME% alice C:\>echo %USERDOMAIN% PRO C:\>echo %USERDNSDOMAIN% PROTECTIGATE.COM # Combine them C:\>echo %USERNAME%@%USERDNSDOMAIN% alice@PROTECTIGATE.COM C:\>
Comments