How Do You Find the Current User in a Windows Environment?
When Running a Command-Line Script, Is It Possible to Get the Name of the Current User? 2 15 Answers You Can Use the Username Variable: %Username% 1 Username...
When running a command-line script, is it possible to get the name of the current user?
15 Answers
You can use the username variable: %USERNAME%
Username:
echo %USERNAME%
Domainname:
echo %USERDOMAIN%
You can get a complete list of environment variables by running the command set from the command prompt.
Just use this command in command prompt
C:\> whoami
It should be in %USERNAME%. Obviously this can be easily spoofed, so don't rely on it for security.
Useful tip: type set in a command prompt will list all environment variables.
%USERNAME% is the correct answer in batch and other in Windows environments.
Another option is to use %USERPROFILE% to get the user's path, like C:\Users\username.
The answer depends on which "command-line script" language you are in.
Cmd
In the old cmd.exe command prompt or in a .bat or .cmd script, you can use the following:
%USERNAME% - Gets just the username.
%USERDOMAIN% - Gets the user's domain.