views:

1405

answers:

3

How to get the current username in Windows Powershell?

+5  A: 

Found it:

[Environment]::UserName

There is also:

[Environment]::UserDomainName
[Environment]::MachineName
Thomas Bratt
A quick and dirty alternative would be `$env:username` to retrieve the user name from the corresponding environment variable.
guillermooo
I think $env:username and [Environment]::UserName are both pointing to the same thing.
Cephas
A: 

When you use the Get-Credential cmdlet, you get a GUI dialog box to enter the credentials. This is the "Common Criteria Certified" way of handling credentials. It is also a pain in the butt at times. If you are an admin, you can alter this and request credentials via the command line as follows:

PS> $key = "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds"

PS> Set-ItemProperty $key ConsolePrompting True

PS> Get-Credential

cmdlet Get-Credential at command pipeline position 1

Supply values for the following parameters:

Credential

User: ntdev\jsnover

Password for user ntdev\jsnover: ******

UserName Password -------- -------- ntdev\jsnover System.Security.SecureString

Enjoy!

ratty
+3  A: 
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
Mark Seemann