本文翻译自:How do I get the current username in Windows PowerShell?
如何在Windows PowerShell中获取当前的用户名?
#1楼
参考:https://stackoom.com/question/8KB2/如何在Windows-PowerShell中获取当前的用户名
#2楼
I found it: 我找到了:
$env:UserName
There is also: 还有:
$env:UserDomain
$env:ComputerName
#3楼
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
#4楼
I didn't see any Add-Type based examples. 我没有看到任何基于Add-Type的示例。 Here is one using the GetUserName directly from advapi32.dll. 这是直接从advapi32.dll使用GetUserName的一种。
$sig = @'
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool GetUserName(System.Text.StringBuilder sb, ref Int32 length);
'@
Add-Type -MemberDefinition $sig -Namespace Advapi32 -Name Util
$size = 64
$str = New-Object System.Text.StringBuilder -ArgumentList $size
[Advapi32.util]::GetUserName($str, [ref]$size) |Out-Null
$str.ToString()
#5楼
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[Environment]::UserName
-@ThomasBratt -
$env:username
-- @Eoin$env:username
-@Eoin -
whoami
-- @galaktorwhoami
- @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
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. 我必须在具有模拟功能的C#应用程序中运行的PowerShell脚本中使用@Mark Seemann的Windows访问令牌方法。
The C# application is run with my user account, and it runs the PowerShell script as a service account. C#应用程序使用我的用户帐户运行,并且将PowerShell脚本作为服务帐户运行。 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脚本的方式受到限制,因此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"
#6楼
In my case, I needed to retrieve the username to enable the script to change the path, ie. 就我而言,我需要检索用户名以使脚本能够更改路径,即。 c:\\users\\%username%\\
. c:\\users\\%username%\\
。 I needed to start the script by changing the path to the users desktop. 我需要通过更改用户桌面的路径来启动脚本。 I was able to do this, with help from above and elsewhere, by using the get-location applet. 在上面和其他地方的帮助下,我可以使用get-location小程序来做到这一点。
You may have another, or even better way to do it, but this worked for me: 您可能有另一种甚至更好的方法,但这对我有用:
$Path = Get-Location
Set-Location $Path\Desktop