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

10

12

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?

Derekderelict answered 18/10, 2011 at 15:19 Comment(0)
D
12

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.

Derekderelict answered 18/10, 2011 at 17:32 Comment(4)
We can find the domain name of a computer by running the following commnad from command line. systeminfo | findstr /B /C:”Domain” We can find the logged in user’s domain by using the environment variable ‘Userdomain’. Command for this is given below. echo %userdomain%Quaky
Great! Works in PowerShell as well: ((net user $env:USERNAME /domain | Select-String "Full Name") -replace "Full Name","").Trim()Harilda
I was need to surround %command% with inverted commas in the second line to make it work: ('"%command%"')Benzol
this command doesn't work when you're logged into a different domain account from what your machine is connected to, in an AD forestUndertone
K
5

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
Khrushchev answered 11/1, 2016 at 11:46 Comment(2)
Please note this is formatted for bat or cmd script. This will not work "From command prompt" as OP asked aboveFormer
for /f "usebackq tokens=2,* delims= " %a in (`net user "%USERNAME%" /domain ^| find /i "Full Name"`) do set FULLNAME=%b /// Just change double %% to % and it will works from CMD///Khrushchev
D
4

Here's a derivative of skeletank's response that's slightly cleaner that worked for me... (I tried skeletank's response and it didn't work for me the way it was written, thus my answer below.)

SET TNAME="net user %USERNAME% /domain| FIND /I "Full Name""
FOR /F "tokens=3,4 delims=, " %%A IN ('%TNAME%') DO SET DNAME=%%B %%A

Then you can use 'DNAME' anywhere you need like to ouput with an echo or validate with an 'IF' statement. You can flip the '%%B' and '%%A' tokens around to go back to last name first layout. This saves the additional effort used in skeletank's original response.

Variables:
-TNAME = Temporary Name
-DNAME = Display Name

-Thanks skeletank.

Dote answered 9/5, 2014 at 21:16 Comment(0)
A
2

This should be a clean way using dsquery and dsget without relying on the output of "Full Name", which is language dependent, whereas "fn" seems to work with different language versions of windows.

for /F "tokens=2" %i in ('dsquery user -samid %username% ^| dsget user -L -fn ^| find "fn:"') DO @echo %i

The dsget -L switch gives the listed output "fn: USER" so the only thing left is to grab the second column in the output. find "fn:" is used to get rid of the "dsget succeeded" output.

Augustaugusta answered 23/5, 2014 at 14:36 Comment(2)
win10 execution in cmd results in: username"') DO @echo i was unexpected at this time. looks like it's from dsquery not existing on the machineUndertone
This is formatted for use from command line. Matt, please refer to my answer for difference between command line and script formatting. This particular command requires RSAT installed on the computer you run it from.Former
F
2

The accepted answer (at time of posting) has 4 lines of code, when you only need one to do the same thing:

for /f "tokens=2*" %n in ('net user "%USERNAME%" /domain^|FINDSTR /C:"Full Name"') do echo "%o"

This will work even if the Full Name has more than 2 parts. Works on Windows workstations "out of the box" and does not require admin rights.

NOTE: Remember to change %n and %o to %%n and %%o,respectively, when using in a batch file.

Explanation of the command:

"tokens=2* specifies that the only tokens it will pay attention to are the 2nd token (assigned to %n), and everything after it (assigned to the next letter:%o)

The default delims is space so we don't need to define it, but if we need to manually define it it'd be as follows: "tokens=2* delims= ". Because the space is a delimiter, it will ignore all leading spaces.

The pipe (|) is escaped by a carrot (^), telling the command processor to ignore it initially.

I also prefer to use FINDSTR instead of FIND because (in my testing) I've found it to be the faster command, which can make a difference if you're using it frequently. FINDSTR /C:"Full Name" could be easily replaced with FIND "Full Name" if you preferred.

Former answered 7/1, 2016 at 4:17 Comment(3)
win10 cmd execution in a .bat results in: USERNAME"Full Name"') was unexpected at this time.Undertone
doesn't work if the user is on a different domain from what they are currently connected to (but still in AD forest w/ trust), The request will be processed at a domain controller for domain XXX.XXX.XXX. The user name could not be found. More help is available by typing NET HELPMSG 2221.Undertone
OP question asked for command "From the command prompt". My answer is formatted as such. However, I specifically state how to format it for .bat script should you want to use it for that. Please see the "NOTE".Former
W
0

I built a batch file to do a few things quickly by selecting an action using set p and goto. I'm leaving out the unnecessary parts.

@echo off

:begin

echo 5. Find who is on remote PC right now

echo.

set /p a=

IF %a%==5 (goto whoson)

REM Whos on
:whoson
set /p remotecomputername=Enter computer name to see the current user: 
wmic /node:%remotecomputername% computersystem get username
pause
goto begin

I hope it helps.

Witter answered 22/8, 2013 at 1:9 Comment(1)
just results in an infinite loop of prompts with error: Invalid Global Switch.Undertone
B
0

If you want it for human use - meaning you don't need to automatically parse it so you get just the full name - you can just type net user <username> /domain at a command prompt, and read the "Full Name" field.

Reference: Getting detailed domain user info from the command line.

Burson answered 20/7, 2015 at 20:56 Comment(1)
link broken, and this command doesn't work when you're logged into a different domain account from what your machine is connected to, in an AD forestUndertone
P
0

From remote command prompt (I usually establish this by using PSexec.exe), then simply type "query user"

enter image description here

Pennsylvanian answered 20/3, 2018 at 16:52 Comment(1)
It requires some more explanationBarkentine
D
0

simplified version from skeletank

for /F "tokens=3,4 delims=, " %%A in ('net user "%USERNAME%" /domain ^|findstr /C:"Full Name"') do (set _FullName=%%B.%%A)

That makes Doe, John into John.Doe

Disfeature answered 30/3, 2022 at 16:34 Comment(1)
This seems to be made to do the opposite of the intended action.Crescantia
L
0
systeminfo | findstr /B /C:"Domain"

Reference: Find domain name from command line

Lothario answered 4/7, 2022 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.