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?

#11
I find easiest to use: cd $home\Desktop\

will take you to current user desktop
--------------------------------------------------------

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

#12
$username=( ( Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username ) -split '\\' )[1]

`$username`


The _second_ _username_ is for display only purposes only if you copy and paste it.
Reply

#13
On Windows, you can:

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
Reply

#14
Sometimes the Username attribute has no data in Win32_ComputerSystem even though there's a user signed in. What works for me is to use *quser* and parse the output. It's not perfect, but it works. E.g.:

$quserdata = @()
$quserdata = quser
$userid = ($quserdata[1] -split ' ')[1]
$userid



Note: if this is run as the user who is logged in, *quser* adds '>' symbol to the output. Then you need to get rid of that symbol, but mostly this is needed for code run as system or another account than the one that is logged in.

Reply

#15
I thought it would be valuable to summarize and compare the given answers.

If you want to access the **[environment variable][1]**:
---------------------------------------------------

*(easier/shorter/memorable option)*

- `[Environment]::UserName` -- @ThomasBratt
- `$env:username` -- @Eoin
- `whoami` -- @galaktor

*************

If you want to access the **[Windows access token][2]**:
----------------------------------------------------------

*(more dependable option)*

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

- `$(Get-WMIObject -class Win32_ComputerSystem | select username).username` -- @TwonOfAn on [this other forum](

[To see links please register here]

)

****
Comparison
--------------

@Kevin Panko's comment on @Mark Seemann's answer deals with choosing one of the categories over the other:

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

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:**

$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:**

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"

(you may need a hyphen before `noprofile`, like so)

Start-Process -Credential $cred -FilePath 'powershell.exe' -ArgumentList '-noprofile','-Command',"$script"

[1]:

[To see links please register here]

[2]:

[To see links please register here]


Reply

#16
I found it:

[Environment]::UserName
$Env:UserName

There is also:

$Env:UserDomain
$Env:ComputerName
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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