Home » PowerShell Tips » Get Current User name in PowerShell

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. It’s very important for the admin as part of best security practice to know the user logged in to the Windows system and get the currently logged-in user name in the domain environment.

WindowsIdentity class has GetCurrent() method, whoami and using Win32_ComputerSystem class, you can get current username in PowerShell.

In the 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 the below command to get current username

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

The 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 below

whoami

To get current user name and SID using whoami, run the 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 the whoami command, use below

whoami /fqdn

The output of the above command gets the 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 the 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 the 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 the .Net environment class. Environment class has UserName property to get current user name, use the command as below

[System.Environment]::UserName

The output of the above command to get user name on the current system as below

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

Conclusion

I hope the above article to get the current user name in PowerShell is 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 the ShellGeek home page.