Windows Command Equivalent to Unix “whoami”: Getting Current User in CMD/PowerShell


2 views

For developers transitioning from Unix/Linux to Windows environments, a common question arises: how to retrieve the current logged-in user information. The Unix whoami command has direct equivalents in Windows, available through both Command Prompt (CMD) and PowerShell.

Windows provides several ways to achieve this:


:: Command Prompt
whoami
echo %username%
set user

# PowerShell
$env:USERNAME
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
whoami.exe

The whoami.exe utility (available since Windows XP) offers Unix-like functionality:


whoami                        :: NTLM logon name (DOMAIN\USERNAME)
whoami /upn                   :: User Principal Name (user@domain.com)
whoami /priv                  :: Current privileges
whoami /groups                :: Group memberships
whoami /all                   :: Complete user information

For scripting scenarios, these methods provide more flexibility:


@echo off
for /f "tokens=2 delims=:" %%A in ('whoami /upn') do set "upn=%%A"
echo User Principal Name: %upn%

# PowerShell function
function Get-CurrentUser {
    $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    return [PSCustomObject]@{
        Name = $identity.Name
        SID = $identity.User.Value
        Groups = $identity.Groups | ForEach-Object { $_.Translate([System.Security.Principal.NTAccount]).Value }
    }
}
Get-CurrentUser

When dealing with impersonation or service accounts, be aware of these nuances:


:: Shows the actual security context, not original logged-in user
whoami /groups /fo list

For service accounts running under different credentials, you might need:


[System.Security.Principal.WindowsIdentity]::GetCurrent().ImpersonationLevel

For Python scripts that need to work across platforms:


import os
import getpass

def get_current_user():
    if os.name == 'nt':
        return os.getenv('USERNAME')
    else:
        return getpass.getuser()

print(f"Current user: {get_current_user()}")

Surprisingly, Windows actually includes a native whoami.exe command that functions nearly identically to its Unix counterpart:

C:\> whoami
mydomain\jsmith

This executable comes pre-installed on modern Windows systems (Windows XP SP2 and later) and is located in %WINDIR%\System32.

For scenarios where whoami.exe might not be available or when you need more detailed information:

Using Environment Variables

C:\> echo %USERNAME%
jsmith

C:\> echo %USERDOMAIN%
mydomain

PowerShell Options

PowerShell provides several robust alternatives:

PS C:\> $env:USERNAME
jsmith

PS C:\> [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
mydomain\jsmith

PS C:\> (Get-WmiObject Win32_ComputerSystem).UserName
mydomain\jsmith

Here's a portable batch solution that works across Windows versions:

@echo off
:: whoami.bat - Windows whoami alternative
if exist "%WINDIR%\System32\whoami.exe" (
    whoami
) else (
    echo %USERDOMAIN%\%USERNAME%
)

For system administrators needing to check current user in scripts:

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /s | find "ProfileImagePath"

When working with elevated privileges or impersonation, these commands become particularly useful:

whoami /groups
whoami /priv
whoami /all

The /all flag provides comprehensive security context information including:

  • SID (Security Identifier)
  • Group memberships
  • Privileges
  • Logon ID

For scripts that need to run on both Unix and Windows systems:

@echo off
:: Detect OS and run appropriate command
if "%OS%"=="Windows_NT" (
    if exist "%WINDIR%\System32\whoami.exe" (
        whoami
    ) else (
        echo %USERDOMAIN%\%USERNAME%
    )
) else (
    sh -c "whoami"
)