Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 623 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I get the current username in Windows PowerShell?

#1
How do I get the current username in Windows PowerShell?

Reply

#2
`[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__
Reply

#3
I'd like to throw in the [whoami][1] command, which basically is a nice alias for doing `%USERDOMAIN%\%USERNAME%` as proposed in other answers.

Write-Host "current user:"
Write-Host $(whoami)


[1]:

[To see links please register here]

Reply

#4
`$env:username` is the easiest way
Reply

#5
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:

$Path = Get-Location

Set-Location $Path\Desktop

Reply

#6
If you're used to batch, you can call

$user=$(cmd.exe /c echo %username%)

This basically steals the output from what you would get if you had a batch file with just "echo %username%".
Reply

#7
I didn't see any [Add-Type][1] based examples. Here is one using the GetUserName directly from advapi32.dll.

$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()

[1]:

[To see links please register here]


Reply

#8
Just building on the work of others here:

[String] ${stUserDomain},[String] ${stUserAccount} = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.split("\")
Reply

#9
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.

I'd upvote Mark Seemann's answer:
[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`.

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.
Reply

#10
Now that [PowerShell Core][1] (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.

`[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.

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through