从命令提示符获取当前 Windows 域用户的显示名称

发布于 2024-12-10 14:05:59 字数 61 浏览 0 评论 0原文

从命令提示符中,如何获取当前登录的域用户的友好显示名称(即“John Doe”而不是“john.doe”)?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

扎心 2024-12-17 14:06:00

从远程命令提示符(我通常使用 PSexec.exe 建立此命令),然后只需键入“query user”

在此处输入图像描述

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

enter image description here

海未深 2024-12-17 14:06:00

skeletank 的简化版本

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

这使得 Doe、John
进入约翰·多伊

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

初熏 2024-12-17 14:06:00
systeminfo | findstr /B /C:"Domain"

参考:从命令行查找域名

systeminfo | findstr /B /C:"Domain"

Reference: Find domain name from command line

另类 2024-12-17 14:05:59

这是我在批处理文件中使用 net 命令和 find 命令完成的一种棘手方法:

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

第一行将我们要执行的命令存储在变量中。它从环境变量中提取用户名,并将其传递给 net user 命令以及 /domain 参数,告诉它从当前域中提取。然后它将结果(当前用户的一堆数据)传输到 find 方法,该方法将仅提取我们想要的属性。查找结果的格式为“Full Name John Doe”。第二行将执行命令并将结果放入变量fullNameText中。第三行将删除结果的“全名”部分,并以“John Doe”结尾。第四行的 for 循环将从结果中删除所有前导空格,最终在 fullName 变量中得到“John Doe”。

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.

浮萍、无处依 2024-12-17 14:05:59

Lectrode 在一个字符串中的答案将如下所示:

for /f "usebackq tokens=2,* delims= " %%a in (`net user "%USERNAME%" /domain ^| find /i "Full Name"`) do set FULLNAME=%%b

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
江湖彼岸 2024-12-17 14:05:59

这是 skeletank 响应的衍生版本,它对我来说稍微干净一些......(我尝试了 skeletank的响应,它对我来说不起作用,因为它是这样写的,因此我的答案如下。)

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

然后你可以使用'DNAME' 任何您需要的地方,例如使用 echo 进行输出或使用 'IF' 语句进行验证。您可以翻转“%%B”和“%%A”标记以返回姓氏在前的布局。这节省了 skeletank 原始响应中使用的额外工作。

变量:
-TNAME = 临时名称
-DNAME = 显示名称

-感谢skeletank

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.

绻影浮沉 2024-12-17 14:05:59

这应该是使用 dsquery 和 dsget 的一种干净方法,而不依赖于“全名”的输出,“全名”是依赖于语言的,而“fn”似乎适用于不同语言版本的 Windows。

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

dsget -L 开关给出列出的输出“fn: USER”,因此剩下的唯一事情就是获取输出中的第二列。
find "fn:" 用于消除“dsget successed”输出。

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.

回梦 2024-12-17 14:05:59

接受的答案(在发布时)有 4 行代码,当您只需要一行代码来做同样的事情时:

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

即使全名有超过 2 个部分,这也将起作用。 “开箱即用”在 Windows 工作站上工作,不需要管理员权限。

注意:记住将 %n%o 分别更改为 %%n%%o,当在批处理文件中使用。

命令说明:

"tokens=2* 指定它只关注第二个标记(分配给 %n ),以及其后的所有内容(分配给下一个字母:%o)。

默认的 delims 是空格,因此我们不需要定义它,但如果我们需要手动定义它如下: "tokens=2* delims= "。因为空格是分隔符,所以它将忽略所有前导空格。

管道 (|) 由胡萝卜 () 转义。 >^),告诉命令处理器最初忽略它,

我也更喜欢使用 FINDSTR 而不是 FIND 因为(在我的测试中)我发现。它是更快的命令,如果如果您经常使用它,FINDSTR /C:"Full Name" 可以轻松替换为 FIND "Full Name"

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.

恋你朝朝暮暮 2024-12-17 14:05:59

我构建了一个批处理文件,通过使用 set p 和 goto 选择一个操作来快速执行一些操作。我省略了不必要的部分。

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

往事风中埋 2024-12-17 14:05:59

如果您希望它供人类使用 - 这意味着您不需要自动解析它,因此您只需输入全名 - 您只需输入 net user; /domain 在命令提示符处,然后读取“全名”字段。

参考:从命令行获取详细的域用户信息

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文