How Do I Get the Current Username in Windows Powershell?

How do I get the current username in Windows PowerShell?

14 Answers

I found it:

$env:UserName

There is also:

$env:UserDomain
$env:ComputerName
2

On Windows, you can:

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
6

I thought it would be valuable to summarize and compare the given answers.

If you want to access the environment variable:

(easier/shorter/memorable option)

  • [Environment]::UserName -- @ThomasBratt
  • $env:username -- @Eoin
  • whoami -- @galaktor

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.