Use environment variable %username% to get the currently logged-in user name using cmd in the Windows system. Environment variables store the current user logged-in information.
Use the whoami command in cmd to get the current user name in windows 10 or other Windows OS. whoami command prints the current username when run.
In this article, we will discuss how to get the current username in Windows using Cmd and PowerShell.
CMD – Get Current User Name using %username%
To find the currently logged-in user name in the Windows operating system, use the %username%
environment variable.
%username% stores the current user, use echo to print the current user name. It gets the user name without the domain name.
echo %username%
In the above command, %username% gets the currently logged-in user name and prints its user name without the domain name.
You can also use PowerShell to get the currently logged-in username using the environment variable %username%.
$Env:USERNAME
In the above PowerShell script, we used the variable $Env: with the environment variable USERNAME to find the current username.
The output of the above PowerShell script is:
PS D:\> $Env:USERNAME
ShellGeek
PS D:\>
Cool Tip: How to get the current username in PowerShell!
Find the Current Logged in User using whoami
Using the whoami command, you can get the currently logged-in user in the Windows system. The whoami command prints the username with the domain name.
whoami
The output of the above command in CMD to find the current username in Windows is:
C:\>whoami
ShellPro\ShellGeek
C:\>
You can also use PowerShell to print the current username using the whoami command.
PS D:\> whoami
The output of the above PowerShell script gets the username in windows with the domain name.
PS D:\> whoami
ShellPro\ShellGeek
PS D:\>
Conclusion
I hope the above article to get currently logged-in user in the Windows OS using the %username% environment variable and whoami command is helpful to you.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.