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:
- whoami.
- Env PowerShell Drive.
- $Env Variable.
- Get-CimInstance.
- Get-WMIObject.
- .NET Environment Class.
- .NET WindowsIdentity Class.
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:
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:
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:
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:
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:
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:
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:
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.