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?
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.
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
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 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.
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.
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.
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.
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.
From remote command prompt (I usually establish this by using PSexec.exe), then simply type "query user"
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
systeminfo | findstr /B /C:"Domain"
Reference: Find domain name from command line
© 2022 - 2024 — McMap. All rights reserved.