Powershell

How to Get the Current Username in Windows PowerShell?

PowerShell is the Windows command line shell utilized for the automation of tasks and also for configuration management. When multiple accounts are available on Windows, you can check which account is currently logged in. For that reason, PowerShell will help you get Windows’s current username. It also offers several commands in its inventory that will help to get the current username.

This tutorial will observe several approaches to resolve the stated query.

How to Get/Know the Current Username in Windows PowerShell?

These are the given methods that can be approached to get the current username in Windows:

Method 1: Getting Current Username Using “whoami” Command

The “whoami” command is utilized in Windows and Unix operating systems to check the current username.

In order to get the current username, first launch “PowerShell” from the Start menu and execute it as follows:

>(whoami).Split('\')[1]

Method 2: Getting Current Username Using “Env” PowerShell Drive

Env” is a drive that is available and accessed only in PowerShell. It is used to store the environment variables on your system. This command can also be utilized to get the current username. To do so, run the “Get-ChildItem” command and get the “USERNAME” from the Env drive:

> Get-ChildItem Env:\USERNAME

Method 3: Getting Current Username Using “$Env” Variable

The Env drive can be used as the variable also to get the current username of the system as follows:

> $env:username

Method 4: Getting Current Username Using “Get-CimInstance” Command

Another cmdlet that can be used to retrieve the current username is “Get-CimInstance”. It is the Win32_ComputerSystem command in Windows.

To get the current username using the discussed command, type it out in PowerShell:

>(Get-CimInstance -ClassName Win32_ComputerSystem).Username.Split('\')[1]

Method 5: Getting Current Username Using “Get-WMIObject” Command

Get-WMIObject” is the Win32_ComputerSystem command. It is the older version command that is supported on PowerShell 5.1.

You can also utilize it for verifying the username:

>(Get-WMIObject -ClassName Win32_ComputerSystem).Username.Split('\')[1]

Method 6: Getting Current Username Using “.NET” Environment Class

Using the “.NET” environment class is another approach to get the current username in Windows PowerShell:

>[System.Environment]::UserName

Method 7: Getting Current Username Using “.NET” WindowsIdentity Class

Another .NET command that can be used to get the current username in PowerShell is .NET WindowsIdentity Class, utilized as follows:

>([System.Security.Principal.WindowsIdentity]::GetCurrent().Name).Split('\')[1]

That was all about getting the current username in Windows PowerShell.

Conclusion

To get the current username in PowerShell, type and execute the “whoami” command. Moreover, there are other commands that can also be used to get the current username, such as the “$Env” variable, “Get-CimInstance”, “Get-WMIObject”, “.NET Environment Class”, “.NET WindowsIdentity Class”, or “Env” Drive command. This tutorial has presented a detailed procedure to resolve the stated query.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.