A lot of users are confused about the PowerShell get current username operation. Are you also searching for a guide? In this post, MiniTool will walk you through detailed steps to get user name in PowerShell Windows 11/10/8/7.
As it’s well known to us, PowerShell powerful Windows command-line shell and scripting environment tool. It can be used to deal with many different works such as PowerShell change directory, PowerShell get user group membership, PowerShell copy files, PowerShell install SSH, PowerShell run exe, and more.
However, a lot of users and even experienced professionals still don’t know these PowerShell cmdlets. How do make PowerShell get logged on user? Here’s a true example from the stackoverflow.com forum.
I need to know the logged user name on Windows 10. How do I get the current username in Windows PowerShell? Please help me.
What Command Can Be Use to Get Current Username in PowerShell
First of all, it’s necessary to figure out what command can help PowerShell get logged on user. After investigating forums and posts, we find there are many ways to get current username in PowerShell Windows 11/10/8/7. Here we summarize them as follows:
- Windows Environment Variables: There are 3 different cmdlets to interact with the environment variables, including Env PowerShell drive, $env variable, and .NET Environment Class.
- Win32_ComputerSystem Class: Under this class, you can use the Get-WMIObject and Get-CimInstance cmdlets to let PowerShell get username.
- .NET WindowsIdentity Class: You can use the GetCurrentName cmdlet to get the current username on Windows.
- Whoami command: It is an executable file that can be used to find the username in the System32 folder.
- Query command: It can be used to list all currently logged-on user names on the remote computer via the Remote Desktop Protocol.
Now, you should have an overall understanding of the PowerShell get users name cmdlets. Let’s see how to run these cmdlets.
PowerShell Get Current Username on Windows 11/10/8/7
According to the above information, we summarize 5 feasible ways to get username PowerShell in Windows 11/10/8/7. You can choose one based on your preference.
Since some cmdlet requires the PowerShell v5.1 and later version, we recommend you keep the PowerShell version up to date. Here you can read this post “How to Update PowerShell Version to v7.2.5 for Windows 10/11”.
# 1. PowerShell Get Current Username via Environment Variables Class
The Windows Environment Variables can detect the username and logged-on user from your system via the following 3 methods.
Way 1. Use Env PowerShell Drive
- Type PowerShell in the search box, and then right-click the Windows PowerShell and select Run as administrator. Then click on Yes to confirm it.
- In the pop-up window, type the following command and hit Enter to get the current logged-on user name.
(Get-ChildItem Env:\USERNAME).Value (It only returns the username value as a string)
Way 2. Use the $Env Variable
Another simple method is to get the username PowerShell using the $Env variable. To do so, you just need to run the $env:username command in the PowerShell window.
Way 3. Use the .NET Environment Class
Like the $env variable, the .NET Environment class enables you to find the current username value in PowerShell. To make PowerShell get all the last logged users, you can run the command below:
In addition, you can use the GetEnvironmentVariable cmdlet with the Environment class to get the current username.
# 2. PowerShell Get Current Username via Win32_ComputerSystem Class
With the Win32_ComputerSystem Class, you can make PowerShell get logged-on users via the Get-WMIObject and Get-CimInstance cmdlets. These cmdlets can help you get information about the username on a local or remote computer via the Windows Management Instrumentation.
To get the current domain and username on a local computer, you can open the PowerShell window and run the following commands:
(Get-WMIObject -ClassName Win32_ComputerSystem).Username
(Get-CimInstance -ClassName Win32_ComputerSystem).Username
If you just want to get only the current username without the domain, you can use the split () parameter:
((Get-WMIObject -ClassName Win32_ComputerSystem).Username).Split('\')
# 3. PowerShell Get Current Username via .NET WindowsIdentity Class
The .NET WindowsIdentity Class can also make PowerShell get users name via the GetCurrentName cmdlet. To do so, you just need to run the command below in the PowerShell window.
To get only the username value, you can use the Split('\') parameter as well:
# 4. PowerShell Get Current Username via the Whoami Command
To get the current username and domain in PowerShell, you just need to run the whoami command. If you just want to get the username only, you can run the (whoami).Split('\') command.
# 5. PowerShell Get Current Username via the Query Command
If you were an administrator or domain user, you can get the logged-on username on a remote computer. For example, if you want to get all logged-on users on a computer named PC1, you can run the qwinsta /server:pc1 command in PowerShell. To show all users with existing log-on sessions, you can run the quser /server:pc1 command.
Further reading: If you enter some issues like file system corruption and low disk space on Windows, don’t worry. MiniTool Partition Wizard can help you fix them easily by checking file system errors, extending/resizing partitions, analyzing disk space, upgrading to a larger hard disk, etc.