如何在 Windows PowerShell 中获取当前用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2085744/
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
How do I get the current username in Windows PowerShell?
提问by Thomas Bratt
How do I get the current username in Windows PowerShell?
如何在 Windows PowerShell 中获取当前用户名?
回答by Thomas Bratt
I found it:
我找到了:
$env:UserName
There is also:
还有:
$env:UserDomain
$env:ComputerName
回答by Mark Seemann
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
回答by alexanderbird
I thought it would be valuable to summarize and compare the given answers.
我认为总结和比较给定的答案会很有价值。
If you want to access the environment variable:
如果要访问环境变量:
(easier/shorter/memorable option)
(更简单/更短/令人难忘的选项)
[Environment]::UserName
-- @ThomasBratt$env:username
-- @Eoinwhoami
-- @galaktor
[Environment]::UserName
-- @ThomasBratt$env:username
-- @Eoinwhoami
-- @galaktor
If you want to access the Windows access token:
如果要访问Windows 访问令牌:
(more dependable option)
(更可靠的选择)
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
-- @MarkSeemann
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
-- @MarkSeemann
If you want the name of the logged in user
如果你想要登录用户的名字
(rather than the name of the user running the PowerShell instance)
(而不是运行 PowerShell 实例的用户名)
$(Get-WMIObject -class Win32_ComputerSystem | select username).username
-- @TwonOfAn on this other forum
$(Get-WMIObject -class Win32_ComputerSystem | select username).username
-- @TwonOfAn 在另一个论坛上
Comparison
比较
@Kevin Panko's comment on @Mark Seemann's answer deals with choosing one of the categories over the other:
@Kevin Panko 对@Mark Seemann 的回答的评论涉及选择其中一个类别:
[The Windows access token approach] is the most secure answer, because $env:USERNAME can be altered by the user, but this will not be fooled by doing that.
[Windows 访问令牌方法] 是最安全的答案,因为 $env:USERNAME 可以由用户更改,但这样做不会被愚弄。
In short, the environment variable option is more succinct, and the Windows access token option is more dependable.
总之,环境变量选项更简洁,Windows访问令牌选项更可靠。
I've had to use @Mark Seemann's Windows access token approach in a PowerShell script that I was running from a C# application with impersonation.
我不得不在一个 PowerShell 脚本中使用@Mark Seemann 的 Windows 访问令牌方法,我是从 C# 应用程序中通过模拟运行该脚本的。
The C# application is run with my user account, and it runs the PowerShell script as a service account. Because of a limitation of the way I'm running the PowerShell script from C#, the PowerShell instance uses my user account's environment variables, even though it is run as the service account user.
C# 应用程序使用我的用户帐户运行,并将 PowerShell 脚本作为服务帐户运行。由于我从 C# 运行 PowerShell 脚本的方式的限制,PowerShell 实例使用我的用户帐户的环境变量,即使它作为服务帐户用户运行。
In this setup, the environment variable options return my account name, and the Windows access token option returns the service account name (which is what I wanted), and the logged in user option returns my account name.
在这个设置中,环境变量选项返回我的帐户名,Windows 访问令牌选项返回服务帐户名(这是我想要的),登录用户选项返回我的帐户名。
Testing
测试
Also, if you want to compare the options yourself, here is a script you can use to run a script as another user. You need to use the Get-Credential cmdlet to get a credential object, and then run this script with the script to run as another user as argument 1, and the credential object as argument 2.
此外,如果您想自己比较选项,这里有一个脚本,您可以使用它以其他用户身份运行脚本。您需要使用 Get-Credential cmdlet 来获取凭证对象,然后运行此脚本,将要作为另一个用户运行的脚本作为参数 1,将凭证对象作为参数 2。
Usage:
用法:
$cred = Get-Credential UserTo.RunAs
Run-AsUser.ps1 "whoami; pause" $cred
Run-AsUser.ps1 "[System.Security.Principal.WindowsIdentity]::GetCurrent().Name; pause" $cred
Contents of Run-AsUser.ps1 script:
Run-AsUser.ps1 脚本的内容:
param(
[Parameter(Mandatory=$true)]
[string]$script,
[Parameter(Mandatory=$true)]
[System.Management.Automation.PsCredential]$cred
)
Start-Process -Credential $cred -FilePath 'powershell.exe' -ArgumentList 'noprofile','-Command',"$script"
回答by Eoin
$env:username
is the easiest way
$env:username
是最简单的方法
回答by galaktor
回答by WaffleSouffle
[Environment]::UserName
returns just the user name. E.g. bob[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
returns the user name, prefixed by its domain where appropriate. E.g. SOMEWHERENICE\bob
[Environment]::UserName
只返回用户名。例如,bob[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
返回用户名,在适当的情况下以其域为前缀。例如,某处\鲍勃
回答by Dave Hull
I have used $env:username
in the past, but a colleague pointed out it's an environment variable and can be changed by the user and therefore, if you really want to get the current user's username, you shouldn't trust it.
我以前用过$env:username
,但是同事指出它是一个环境变量,可以由用户更改,因此,如果您真的想获取当前用户的用户名,则不应该相信它。
I'd upvote Mark Seemann's answer: [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
我赞成 Mark Seemann 的回答:[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
But I'm not allowed to. With Mark's answer, if you need just the username, you may have to parse it out since on my system, it returns hostname\username
and on domain joined machines with domain accounts it will return domain\username
.
但我不允许。根据 Mark 的回答,如果您只需要用户名,您可能需要解析它,因为在我的系统上,它会返回,hostname\username
而在具有域帐户的域加入机器上,它会返回domain\username
.
I would not use whoami.exe
since it's not present on all versions of Windows, and it's a call out to another binary and may give some security teams fits.
我不会使用whoami.exe
它,因为它不是在所有版本的 Windows 上都存在,而且它是对另一个二进制文件的调用,可能会让一些安全团队适应。
回答by Edouard Poor
Now that PowerShell Core(aka v6) has been released, and people may want to write cross-platform scripts, many of the answers here will not work on anything other than Windows.
现在PowerShell Core(又名 v6)已经发布,人们可能想要编写跨平台脚本,这里的许多答案在 Windows 以外的任何东西上都不起作用。
[Environment]::UserName
appears to be the best way of getting the current username on all platforms supported by PowerShell Core if you don't want to add platform detection and special casing to your code.
[Environment]::UserName
如果您不想在代码中添加平台检测和特殊大小写,似乎是在 PowerShell Core 支持的所有平台上获取当前用户名的最佳方式。
回答by Stef
Just building on the work of others here:
只是建立在其他人的工作基础上:
[String] ${stUserDomain},[String] ${stUserAccount} = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.split("\")
回答by clayton.nichols
$username=( ( Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username ) -split '\' )[1]
$username
$username
The secondusernameis for display only purposes only if you copy and paste it.
在第二个用户名是仅用于显示的目的只有当你复制和粘贴。