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?


Solution 1:

You can use the username variable: %USERNAME%

Solution 2:

Username:

echo %USERNAME%

Domainname:

echo %USERDOMAIN%

You can get a complete list of environment variables by running the command set from the command prompt.

Solution 3:

Just use this command in command prompt

C:\> whoami

Solution 4:

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.