如何在 Windows 环境中找到当前用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1607271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:51:08  来源:igfitidea点击:

How do you find the current user in a Windows environment?

windowsbatch-file

提问by Geo

When running a command-line script, is it possible to get the name of the current user?

运行命令行脚本时,是否可以获取当前用户的名称?

回答by JRL

You can use the username variable: %USERNAME%

您可以使用用户名变量: %USERNAME%

回答by Magnus Lindhe

Username:

用户名:

echo %USERNAME%

Domainname:

域名:

echo %USERDOMAIN%

You can get a complete list of environment variables by running the command setfrom the command prompt.

您可以通过set从命令提示符运行命令来获取环境变量的完整列表。

回答by Freeman

Just use this command in command prompt

只需在命令提示符中使用此命令

C:\> whoami

回答by the_mandrill

It should be in %USERNAME%. Obviously this can be easily spoofed, so don't rely on it for security.

它应该在%USERNAME%. 显然这很容易被欺骗,所以不要依赖它来保证安全。

Useful tip: type setin a command prompt will list all environment variables.

有用的提示:set在命令提示符下键入将列出所有环境变量。

回答by Recon

%USERNAME%is the correct answer in batch and other in Windows environments.

%USERNAME%是 Windows 环境中批处理和其他的正确答案。

Another option is to use %USERPROFILE%to get the user's path, like C:\Users\username.

另一种选择是用于%USERPROFILE%获取用户的路径,例如C:\Users\username.

回答by BlueBearr

%USERNAME% will get you the username of the currently running process. Depending on how you are running your batch file, this is not necessarily the same as the name of the current user. For example, you might be running your batch file through a scheduled task, from a service, etc.

%USERNAME% 将为您提供当前正在运行的进程的用户名。根据您运行批处理文件的方式,这不一定与当前用户的名称相同。例如,您可能正在通过计划任务、服务等运行批处理文件。

Here is a more sure way of getting the username of the currently logged on user by scraping the name of the user that started the explorer.exe task:

这是通过抓取启动 explorer.exe 任务的用户名来获取当前登录用户的用户名的更可靠方法:

for /f "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq explorer.exe" /FO LIST /V') do if /i "%%a %%b"=="User Name:" set _currdomain_user=%%c
for /f "TOKENS=1,2 DELIMS=\" %%a in ("%_currdomain_user%") do set _currdomain=%%a & set _curruser=%%b

回答by Chad Harrington Mitchell

I use this method in writing batch files for testing.

我在编写批处理文件时使用这种方法进行测试。

echo %userdomain%\%username%

Since you must include the password in plain text if authentication is required, I will only use it in a completely private environment where other users cannot view it or if a user seeing the password would bear no consequences.

由于如果需要身份验证,您必须以纯文本形式包含密码,因此我只会在其他用户无法查看它的完全私密的环境中使用它,或者如果用户看到密码不会承担任何后果。

Hope this helps someone out.

希望这可以帮助某人。

回答by Chris K

It's always annoyed me how Windows doesn't have some of more useful little scripting utilities of Unix, such as who/whoami, sedand AWK. Anyway, if you want something foolproof, get Visual Studio Express and compile the following:

Windows 没有一些更有用的 Unix 小脚本实用程序,例如who/ whoamisedAWK,这总是让我很恼火。无论如何,如果您想要万无一失的东西,请获取 Visual Studio Express 并编译以下内容:

#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv) {
    printf("%s", GetUserName());
}

And just use that in your batch file.

只需在您的批处理文件中使用它。

回答by Jean-Fran?ois Larvtheitroade

In most cases, the %USERNAME% variable will be what you want.

在大多数情况下, %USERNAME% 变量将是您想要的。

echo %USERNAME%

However, if you're running an elevated cmd shell, then %USERNAME% will report the administrator name instead of your own user name. If you want to know the latter, run:

但是,如果您运行的是提升的 cmd shell,那么 %USERNAME% 将报告管理员名称而不是您自己的用户名。如果你想知道后者,运行:

for /f "tokens=2" %u in ('query session ^| findstr /R "^>"') do @echo %u

回答by deadlydog

The answer depends on which "command-line script" language you are in.

答案取决于您使用的是哪种“命令行脚本”语言。

Cmd

命令

In the old cmd.execommand prompt or in a .bator .cmdscript, you can use the following:

在旧的cmd.exe命令提示符或 a .bator.cmd脚本中,您可以使用以下内容:

%USERNAME%- Gets just the username.

%USERNAME%- 只获取用户名。

%USERDOMAIN%- Gets the user's domain.

%USERDOMAIN%- 获取用户的域。

PowerShell

电源外壳

In the PowerShell command prompt or a .ps1or .psm1script, you can use the following:

在 PowerShell 命令提示符或 a.ps1.psm1脚本中,您可以使用以下内容:

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name- Gives you the fully qualified username (e.g. Domain\Username). This is also the most secure method because it cannot be overridden by the user like the other $Envvariables below.

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name- 为您提供完全限定的用户名(例如域\用户名)。这也是最安全的方法,因为它不能像$Env下面的其他变量一样被用户覆盖。

$Env:Username- Gets just the username.

$Env:Username- 只获取用户名。

$Env:UserDomain- Gets the user's domain.

$Env:UserDomain- 获取用户的域。

$Env:ComputerName- Gets the name of the computer.

$Env:ComputerName- 获取计算机的名称。