190

When running a command-line script, is it possible to get the name of the current user?

Freeman
  • 9,464
  • 7
  • 35
  • 58
Geo
  • 93,257
  • 117
  • 344
  • 520
  • Related (from .NET): *[How do I get the current username in .NET using C#?](http://stackoverflow.com/questions/1240373)* – Peter Mortensen May 24 '16 at 12:38
  • 1
    @PeterMortensen, I don't think that thread is super related to this one. One is about using **.NET** and another is about using the **cmd** API. Other than the fact that they both want the same info, there are certainly tons of other APIs that also do a similar thing – KyleMit Jun 02 '20 at 16:14

15 Answers15

249

You can use the username variable: %USERNAME%

George Stocker
  • 57,289
  • 29
  • 176
  • 237
JRL
  • 76,767
  • 18
  • 98
  • 146
115

Username:

echo %USERNAME%

Domainname:

echo %USERDOMAIN%

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

Magnus Lindhe
  • 7,157
  • 5
  • 48
  • 60
75

Just use this command in command prompt

C:\> whoami
Freeman
  • 9,464
  • 7
  • 35
  • 58
  • 1
    This is a good answer, but there are some caveats. If I enter `whoami`, I get `desktop-machine\bballdave025`. There are two parts, seen via: 1) `echo %USERNAME%`, result `bballdave025`; 2) `echo %USERDOMAIN%`, result `DESKTOP-MACHINE`. I guess one could say the 'complete username' is available via `echo %USERDOMAIN%\%USERNAME%` (this result, `DESKTOP-MACHINE\bballdave025` , matches that of `whoami`, ignoring case), or even via `echo %USERNAME%@%USERDOMAIN%`, result `bballdave025@DESKTOP-MACHINE`. _It all depends on what the user (for us, the OP) needs_, i.e. if the domain part be important. – bballdave025 May 21 '20 at 19:46
49

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.

the_mandrill
  • 29,792
  • 6
  • 64
  • 93
  • 2
    Your 'net config Workstation | find "User name" ' comment is very useful. It should have been an answer rather than just a comment – Alpay Aug 25 '15 at 07:26
22

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Recon
  • 331
  • 1
  • 3
  • 7
15

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.

PowerShell

In the PowerShell command prompt or a .ps1 or .psm1 script, you can use the following:

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name - Gives you the fully qualified username (e.g. Domain\Username). This is also the most secure method because it cannot be overridden by the user like the other $Env variables below.

$Env:Username - Gets just the username.

$Env:UserDomain - Gets the user's domain.

$Env:ComputerName - Gets the name of the computer.

Any Shell

whoami - Gets the user's domain and username in the format "domain\username".

This also works on Unix systems as well, not just Windows, so it's a nice cross-platform solution.

deadlydog
  • 22,611
  • 14
  • 112
  • 118
  • This is a great answer as this question is 'shell' dependent. There are many shells you can use with Windows. Not just CMD. These days most folks have moved to powershell and there are others out there. https://stackoverflow.com/questions/17349022/alternative-windows-shells-besides-cmd-exe – G_Style Oct 08 '19 at 19:51
10

%USERNAME% will get you the username of the currently running process. Depending on how you are running your batch file, this is not necessarily the same as the name of the current user. For example, you might be running your batch file through a scheduled task, from a service, etc.

Here is a more sure way of getting the username of the currently logged on user by scraping the name of the user that started the explorer.exe task:

for /f "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq explorer.exe" /FO LIST /V') do if /i "%%a %%b"=="User Name:" set _currdomain_user=%%c
for /f "TOKENS=1,2 DELIMS=\" %%a in ("%_currdomain_user%") do set _currdomain=%%a & set _curruser=%%b
BlueBearr
  • 101
  • 2
8

I use this method in writing batch files for testing.

echo %userdomain%\%username%

Since you must include the password in plain text if authentication is required, I will only use it in a completely private environment where other users cannot view it or if a user seeing the password would bear no consequences.

Hope this helps someone out.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
6

In most cases, the %USERNAME% variable will be what you want.

echo %USERNAME%

However, if you're running an elevated cmd shell, then %USERNAME% will report the administrator name instead of your own user name. If you want to know the latter, run:

for /f "tokens=2" %u in ('query session ^| findstr /R "^>"') do @echo %u
5

It's always annoyed me how Windows doesn't have some of more useful little scripting utilities of Unix, such as who/whoami, sed and AWK. Anyway, if you want something foolproof, get Visual Studio Express and compile the following:

#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv) {
    printf("%s", GetUserName());
}

And just use that in your batch file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris K
  • 11,996
  • 7
  • 37
  • 65
4

Just type whoami in command prompt and you'll get the current username.

Benjamin
  • 3,499
  • 8
  • 44
  • 77
2

This is the main difference between username variable and whoami command:

C:\Users\user.name>echo %username%
user.name

C:\Users\user.name>whoami
domain\user.name

DOMAIN = bios name of the domain (not fqdn)
integratorIT
  • 121
  • 3
1

Via powershell (file.ps1) I use the following

$username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name

It returns the name of the user in the "Domain\Username" format. If you just want the username just write

$username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.Split("\")[1]

The advantage is that It works with windows 10 windows 8 server 2016. As far as I remember with also other OS like Win7 etc. (not older) . And yeah via batch you can simply use

 $username = &whoami
Andy McRae
  • 525
  • 7
  • 15
0

In a standard context, each connected user holds an explorer.exe process: The command [tasklist /V|find "explorer"] returns a line that contains the explorer.exe process owner's, with an adapted regex it is possible to obtain the required value. This also runs perfectly under Windows 7.

In rare cases explorer.exe is replaced by another program, the find filter can be adapted to match this case. If the command return an empty line then it is likely that no user is logged on. With Windows 7 it is also possible to run [query session|find ">"].

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

As far as find BlueBearr response the best (while I,m running my batch script with eg. SYSTEM rights) I have to add something to it. Because in my Windows language version (Polish) line that is to be catched by "%%a %%b"=="User Name:" gets REALLY COMPLICATED (it contains some diacritic characters in my language) I skip first 7 lines and operate on the 8th.

@for /f "SKIP= 7 TOKENS=3,4 DELIMS=\ " %%G in ('tasklist /FI "IMAGENAME eq explorer.exe" /FO LIST /V') do @IF %%G==%COMPUTERNAME% set _currdomain_user=%%H
ITMarka
  • 1
  • 1