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:

echo %USERNAME%

Domainname:

echo %USERDOMAIN%

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

1

Just use this command in command prompt

C:\> whoami
1

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.

1

%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.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.