Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get display name of current Windows domain user from a command prompt

From the command prompt, how can I get the friendly display name (that is, "John Doe" instead of "john.doe") of the domain user that is currently logged in?

like image 920
skeletank Avatar asked Oct 18 '11 15:10

skeletank


People also ask

What is my Windows user domain name?

Find your computer name in Windows 10Open the Control Panel. Click System and Security > System. On the View basic information about your computer page, see the Full computer name under the section Computer name, domain, and workgroup settings.


2 Answers

Here is a tricky way that I did it using the net command and the find command in a batch file:

set command=net user "%USERNAME%" /domain | FIND /I "Full Name"

FOR /F "tokens=1 delims=" %%A in ('%command%') do SET fullNameText=%%A
set fullName=%fullNameText:Full Name=%
for /f "tokens=* delims= " %%a in ("%fullName%") do set fullName=%%a

The first line stores the command that we want to execute in a variable. It pulls the username from the environment variables and passes that into the net user command as well as the /domain parameter to tell it to pull from the current domain. Then it pipes the result from that, which is a bunch of data on the current user, to a find method which will pull out only the property that we want. The result of the find is in the format "Full Name John Doe". The second line will execute the command and put the result into the variable fullNameText. The third line will remove the "Full Name" part of the result and end up with " John Doe". The fourth line with the for loop will remove all of the leading spaces from the result and you end up with "John Doe" in the fullName variable.

like image 89
skeletank Avatar answered Sep 28 '22 20:09

skeletank


Lectrode answer in one string will be like this:

for /f "usebackq tokens=2,* delims= " %%a in (`net user "%USERNAME%" /domain ^| find /i "Full Name"`) do set FULLNAME=%%b
like image 26
kgimpel Avatar answered Sep 28 '22 20:09

kgimpel