How to get the current username in Windows Powershell?
相关问题
- How to Debug/Register a Permanent WMI Event Which
- Inheritance impossible in Windows Runtime Componen
- How can I do variable substitution in a here-strin
- how to get running process information in java?
- Is TWebBrowser dependant on IE version?
相关文章
- 在vscode如何用code runner打开独立的控制台窗口,以及设置好调试模式时窗口的编码?
- C#调用PowerShell的问题
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- Warning : HTML 1300 Navigation occured?
- EscapeDataString having differing behaviour betwee
- Bundling the Windows Mono runtime with an applicat
- Windows 8.1 How to fix this obsolete code?
$env:username
is the easiest wayI'd like to throw in the whoami command, which basically is a nice alias for doing
%USERDOMAIN%\%USERNAME%
as proposed in other answers.get-content "cm.txt"
write-host "entr file name" $file = read-host get-content $file
$content = get-content "cm.txt"
$content = get-content "cn.txt" for each ($line in $count) {write-host $line}
In my case, I needed to retrieve the username to enable the script to change the path, ie.
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.You may have another, or even better way to do it, but this worked for me:
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
-- @galaktorIf you want to access the windows access token:
(more dependable option)
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
-- @MarkSeemannIf you want the name of the logged in user
(rather than the name of the user running the PowerShell instance)
$(Get-WMIObject -class Win32_ComputerSystem | select username).username
-- @TwonOfAn on this other forumComparison
@Kevin Panko's comment on @Mark Seemann's answer deals with choosing one of the categories over the other:
In short, the environment variable option is more succinct, and the windows access token option is more dependable.
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. 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. 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.
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.
Usage:
Contents of Run-AsUser.ps1 script: