The answer depends on which "command-line script" language you are in.
In the old cmd.exe
command prompt or in a .bat
or .cmd
script, you can use the following:
%USERNAME%
- Gets just the username.
%USERDOMAIN%
- Gets the user's domain.
In the PowerShell command prompt or a .ps1
or .psm1
script, you can use the following:
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
- Gives you the fully qualified username (e.g. Domain\Username). This is also the most secure method because it cannot be overridden by the user like the other $Env
variables below.
$Env:Username
- Gets just the username.
$Env:UserDomain
- Gets the user's domain.
$Env:ComputerName
- Gets the name of the computer.