Categories: Batch Files

Get current logged in user name command line (CMD)

In Windows OS, we can find the current logged in username from windows command line. The logged in user information is stored in environment variables. So just by printing the value in these environment variables we can get to know the login name.

To know the login name of the currently logged in user we can run the below command.

echo %username%

This works on all releases of Windows OS(Windows XP, Server 2003, Windows Vista and Windows 7).

There is another command whoami which tells us the domain name also.

whoami

Example:

c:\>whoami
cmdline\administrator

Both of these options to find user name can be useful in batch files to write code in such a way that it works for every user. For example, if your batch file need to access a user specific folder, say Application data, then you can refer the directory as ‘C:\users\%username%\appdata‘.

Srini

View Comments

    • run cmd in admin mode and type net user (user name) (12345)
      then your password change to 12345

    • Suggestion to use command:
      net user “%username%” “%random%%random%%random%%random%%random%”
      will change your current users password to something completely random

  • I don't think it would be possible to find the password as it is stored in encrypted form and not in plain text. However, there are lots of third party tools to reset a administrator password if you have forgotten the same.

  • Can the %username% command be used to determine the user logged into a differnent computer. ex I am at computer x and want to know who is logged on to computer y. I can remote to the c drive using RDP \\computery\c$ I would also like to definively likt to know who is logged on to that computer. Possible?

    • No, it does not work that way. %username% always shows the user of the current login session. When you run it on remote computer, I am not sure what you would get but it won't tell you who logged on to the machine.

    • You can use PSEXEC to run a command on another PC. It can be found in the PS Tools package available for download on many sites.

  • Is it possible to get current User Name when it's an AD where each user has an ID?
    For example, if I run whoami or %username% I get "XX00000" but I want to get "Name SecondName". I know windows has this info because I see my full name in Start Menu (Win 7).
    Thanks

    • net user %username% for accounts stored in local or net user %username% /domain for accounts stored on domain.

    • you know this answer...?... anyone please let me know solution for georges' question.. thanks in advance..

    • If you are asking to find the full name or Surname run this as administrator.
      net user
      It will show you all the information about that user, full name, logon time, the group this user belongs to, the time it is allowed to login all of that.

  • When I run whoami from the cmdline I get my username. However I want to find my username from within SQL Server. If I shell out and run whoami I get the sql service user . Any ideas?

Share
Published by
Srini

Recent Posts

Add new user account from command line (CMD)

Adding users from command line is much easier than going through the UI. It saves…

4 years ago

Delete directory from command line [Rmdir]

Do you want to delete a directory from Windows command prompt(CMD)? This post explains how…

4 years ago

TaskKill: Kill process from command line (CMD)

We can kill a process from GUI using Task manager. If you want to do…

4 years ago

Kill Chrome from command line

Chrome is definitely the stable browser compared to other ones and it has been my…

4 years ago

Find windows OS version from command line

Windows has multiple command line utilities that can help find the version of the Windows…

4 years ago

How to check if Windows 32 or 64 bit?

There are multiple ways to check whether a Windows computer is running 32-bit OS or…

7 years ago