Get Current User name in PowerShell

To get current username in PowerShell, use whoami, GetCurrent() method of WindowsIdentity .Net class or Get-WMIObject cmdlet in PowerShell. In PowerShell there are different ways to get current user name.

In this article, I will explain how to get current user name in PowerShell. Its very important for admin to get current logged in user name in domain environment.

WindowsIdentity class has GetCurrent() method, whoami and using Win32_ComputerSystem class, you can get current username in PowerShell. In domain environment it returns, domain\username.

Get Current User Name using WindowsIdentity

WindowsIdentity .Net class has GetCurrent() method to return current window user object. Using Name property, it returns current logged in user name.

Open PowerShell terminal and type below command to get current username

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

Output of above command, get current user as below

PS C:\> [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
SHELLPRO\John.Paul

Cool Tip: How to rename a computer in PowerShell!

PowerShell Get Current User using whoami

In PowerShell, get current user using whoami command get current user name and domain name.

If you want to get current logged in user and domain name, use whoami command as below

whoami

To get current user name and SID using whoami, run below command

whoami /user

Above command get current logged in user on computer and SID as below

PS C:\> whoami /user                                                                                          
USER INFORMATION
----------------

User Name               SID
======================= ===============================================
SHELLPRO\John.Paul S-1-5-21-3215302419-4252522452-139525725-151479

To get user name in fully qualified domain name (FQDN) format using whoami command, use below

whoami /fqdn

Output of above command get current user name in fully qualified domain name format.

Cool Tip: How to get current user full name in PowerShell!

Get Current Logged in User using Get-WMIObject

PowerShell Get-WMIObject cmdlet gets instances of Win32_ComputerSystem class in WMI classes to get current user name in domain environment. It returns domain\username format.

((Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username)) 

Above command, get user name who currently logged on to computer in domain\username format as below

PS C:\> ((Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username))                
SHELLPRO\John.Paul

Cool Tip: How to get computer information for remote computer in PowerShell!

Get Current User name using .Net Environment

You can get current user name using .Net environment class. Environment class has UserName property get current user name , use command as below

[System.Environment]::UserName

Output of above command to get user name on current system as below

PS C:\> [System.Environment]::UserName                                                                        John.Paul

Conclusion

I hope above article to get current user name in PowerShell helpful to you. As discussed above there are different ways like whoami, WindowsIdentity , Get-WMIObject and Environment class to get current logged in user.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on ShellGeek home page.

Leave a Comment