Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
12.1k views
in PowerShell by 3 4 6
edited by
How I can get the current logged-on username in Windows PowerShell without importing any additional modules, I just need to get the current windows username in PowerShell!

1 Answer

1 like 0 dislike
by 159 198 381
selected by
 
Best answer

Get Current User Name in PowerShell

You can easily get the current username in Windows PowerShell using whoami to return domain\username as below

Get current username in Windows PowerShell

There are also many ways to get the current logged on username in Windows PowerShell as the following:

  1. Using System.Security.Principal.WindowsIdentity
  2. Using $env
  3. Using Win32_ComputerSystem

1) Get current logged-on username in Windows PowerShell

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

This example helps you to get the current user who runs the PowerShell domain\username. so if you are run the Powershell as a different user it will get the current user who runs the PowerShell, not the currently logged-on user to the machine.

Output

Get current logged-on username who runs the PowerShell in Windows PowerShell

1) Get current logged-on username in Windows PowerShell

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

This example helps you to get the current user who runs the PowerShell domain\username. so if you are run the Powershell as a different user it will get the current user who runs the PowerShell, not the currently logged-on user to the machine.

Output

Get current logged-on username who runs the PowerShell in Windows PowerShell

2) Get current logged-on username without domain in Windows PowerShell

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name.split("\")[1]

Or

$env:UserName

This example helps you to get the current user who runs the PowerShell without domain.

Output

Get current logged-on username without domain in Windows PowerShell

3) Get domain name in Windows PowerShell

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name.split("\")[0]

Or

$env:UserDomain #not recommened

This example helps you to get only the current domain name.

Output

Get domain name in Windows PowerShell

4) Get current logged-on username to machine in PowerShell

$userInfo=((Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username) -split '\\' )
$userInfo[0]+"\"+$userInfo[1]

This example helps you to get the logged-in user info like Domain Name $userInfo[0] and User Name $userInfo[1] or domain\username $userInfo[0]+"\"+$userInfo[1]. but if you are running the Powershell as a different user it will not get the current user who runs the PowerShell, it only gets the currently logged-on user to the machine.

Output

Powershell Get Domain and UserName in PowerShell using Win32_ComputerSystem


See Also

If you don’t ask, the answer is always NO!
...