Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current username in Windows PowerShell?

How do I get the current username in Windows PowerShell?

like image 806
Thomas Bratt Avatar asked Jan 18 '10 11:01

Thomas Bratt


People also ask

Who is currently logged into a computer PowerShell?

Use the query Command to Get Logged on Users in PowerShell Windows has a built-in command-line tool called the query command to list all the currently logged-on users on a computer. The command also shows us if the user logged on via a remote desktop session or locally to the computer.

How do I see local users in PowerShell?

The Get-LocalUser cmdlet gets local user accounts. This cmdlet gets default built-in user accounts, local user accounts that you created, and local accounts that you connected to Microsoft accounts. The Microsoft. PowerShell.

How do I Run a PowerShell script as current user?

Use the whoami Command to Get Current User in PowerShell The whoami command is an executable file that resides in the %WINDIR%\System32 folder, and the name of the file is whoami.exe . When we run the below command, it results in the username in domain/username format.


2 Answers

I found it:

$env:UserName 

There is also:

$env:UserDomain $env:ComputerName 
like image 60
Thomas Bratt Avatar answered Oct 12 '22 12:10

Thomas Bratt


On Windows, you can:

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name 
like image 28
Mark Seemann Avatar answered Oct 12 '22 12:10

Mark Seemann