I found it:

[Environment]::UserName
$Env:UserName

There is also:

$Env:UserDomain
$Env:ComputerName
Answer from Thomas Bratt on Stack Overflow
🌐
Reddit
reddit.com › r/powershell › using $env:username or other variables in a script.
r/PowerShell on Reddit: Using $env:USERNAME or other variables in a script.
June 14, 2022 -

So I've been writing a script in powershell, and I'd like to make a few alterations to allow people to use it with minimal effort on their part. Part of this includes adjusting the script so that instances of my username in paths are replaced with $env:USERNAME.

For example,

C:\Users\{username}\Downloads\executable.exe

would become

C:\Users\$env:USERNAME\Downloads\executable.exe

and the outcome would be the same.

I've used variables in paths in bash before with 0 issue, so I'm unsure of where I'm going wrong.

I've tried assigning it to a variable and calling it, and assigning $env:USERNAME and including that variable in the path but neither worked.

I've tried assigning to a variable as a string, and using Invoke-command $command but no dice.

How would I execute a script or an executable with a variable in the path name? Is this possible in powershell?

EDIT I asked in more detail on stackexchange, link below https://stackoverflow.com/questions/72615928/using-envusername-or-other-variables-when-executing-a-script Solved by assigning a variable that joined $env:USERPROFILE and the rest of the path.

🌐
Spiceworks
community.spiceworks.com › programming & development
Username variable in Powershell script - Programming & Development - Spiceworks Community
March 19, 2019 - Hey all, it’s your friendly PowerShell newbie again. I’ll be picking up some PowerShell books here in the next few weeks, but until then, I am tinkering away with the Internet at my side. I have another learning scenario that I was wondering if I can incorporate a username variable into, but I’ve had no luck getting something put together.
🌐
Microsoft Learn
social.technet.microsoft.com › Forums › office › en-US › 5cb1bf92-0f1a-4169-a72f-d2a9e40ac432 › username-variable-in-powershell
%username% variable in Powershell | Microsoft Learn
April 1, 2022 - PS C:\ get-childitem -path "\ums4455\($env:USERNAME)" -recurse -filter cache get-childitem : Cannot find path '\ums4455\my.account' because it does not exist.
🌐
Reddit
reddit.com › r/powershell › username environment variable missing
r/PowerShell on Reddit: Username Environment Variable Missing
February 1, 2021 -

This is a new one for me. I was debugging a failing script and found on some of our computers the $env:username variable isn't registered. I sign into the computer and I can see USERNAME listed as a System Variable and I can open up Command Prompt and use the %Username% variable but in PowerShell, it comes back as empty.

Has anyone run across this one or know how to recreate it for all users?

I'm thinking something along these lines maybe?

[Environment]::SetEnvironmentVariable(Username, someamazingthinghere)

🌐
MiniTool Partition Wizard
partitionwizard.com › home › partition manager › powershell get current username on windows 11/10/8/7 [full guide]
Advanced Guide: PowerShell Get Current Username on Windows
December 4, 2024 - To do so, you just need to run the $env:username command in the PowerShell window. Way 3. Use the .NET Environment Class · Like the $env variable, the .NET Environment class enables you to find the current username value in PowerShell.
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › how to get the current user name in powershell?
How to Get the Current User Name in PowerShell? - SharePoint Diary
September 19, 2025 - If you just need the current user’s ... ... This command pulls the username of the person logged in and stores it in the $currentUser variable....
Find elsewhere
🌐
Reddit
reddit.com › r/powershell › how to make my powershell script reference the current user account who is running it?
r/PowerShell on Reddit: How to make my powershell script reference the current user account who is running it?
September 26, 2022 -

Part of my script involves making yourself a site admin for OneDrive data (set-spouser), then adding the manager, then removing yourself. Since I've always been the only one using this script, I've hardcoded my username into the script. However, other people will be using this script.

When it comes to connecting to exchange online or other admin portals, it's easy to do. You just leave the -userprincipalname field empty, and you get a popup box asking to enter your username, then your password. How do you "leave empty" which account you want to make a site admin for OneDrive data?

🌐
Delft Stack
delftstack.com › home › howto › powershell › powershell get current user
How to Get Current User in PowerShell | Delft Stack
February 2, 2024 - The Environment class does have a Username field, which has the same value as the $env variable: the present user’s username. Execute the PowerShell command below to obtain the value of the username field.
🌐
Spiceworks
community.spiceworks.com › programming & development
Get "DOMAIN\username" into a powershell script where username is a variable too - Programming & Development - Spiceworks Community
January 29, 2020 - Hello, I try to create a script which lists where a user has rights on Exchange 2013 mailboxes. So far: when I manualy typ: Get-Mailbox | Get-MailboxPermission | where {$_.user.tostring() -eq “DOMAIN\username” }|select AccessRights, Identity everything works…
🌐
Adam the Automator
adamtheautomator.com › powershell-get-current-user
Using the Dynamic PowerShell to Get Current Users
So how do you use the Environment class in PowerShell to get the current user? Like the $env variable, the Environment class has a Username property, whose value is the current user’s username.
Published   October 6, 2022
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 740046 › powershell-making-folder-using-env-username
Powershell - making folder using $env:username - Microsoft Q&A
Copy-Item -Path "C:\Temp$($env:USERNAME)\Edge" -Destination "$env:USERPROFILE\AppData\Local\Microsoft\Edge\User Data\Default\Bookmarks"'
🌐
GitHub
github.com › microsoft › terminal › issues › 16200
RunAs Administrator returning "SYSTEM" as $env:USERNAME in PowerShell · Issue #16200 · microsoft/terminal
October 19, 2023 - The screenshot included shows PWSH 7.3.8 running outside Terminal (top left) and inside Terminal (top right) and Windows PowerShell 5.1 running outside Terminal (bottom left) and inside Terminal (bottom right). The output of $env:USERNAME (which is the expected output) has been redacted in the screenshots and the prompt customized to prevent data leak - but this occurs with no customizations and with a fresh installation of Terminal from the Windows Store.
Author   mpearon
🌐
Rene Nyffenegger
renenyffenegger.ch › notes › Windows › dirs › Users › username › index
%USERPROFILE%
The value of %userprofile% usually is c:\Users\username (username being lowercase of %username%?) Apparently, in Windows 10, the variable %CSIDL_PROFILE% is equivalently used for %USERPROFILE%. ... The junctions' (aka reparse points) name and target can be shown in cmd.exe with dir /al. %USERPROFILE% also stores the registry hive for the current user (HKEY_CURRENT_USER) in the file %USERPROFILE%\NTUSER.DAT. In PowerShell, the home directory of a user is stored in the automatic variable $home.
🌐
Ironman Software
forums.ironmansoftware.com › powershell universal
Using $username in Pages - PowerShell Universal - Ironman Software Forums
October 7, 2022 - v2.12.5 Using IIS Anyone able to pass $username off to a script using Pages? I see the variable when going to edit, can use it 50% of the time in the form display but never to pass it to the script. Steps to reproduce: Create simple test script to take in username and write-host param($username) write-host "username is $username" start-sleep -Seconds 5 Create a Test Page Create a simple form, link the button to the script and put $username as the title, create a hidden username field with...
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.security › get-credential
Get-Credential (Microsoft.PowerShell.Security) - PowerShell | Microsoft Learn
When you enter the requested information, the cmdlet creates a PSCredential object representing the credentials of the user and saves it in the $c variable. You can use the object as input to cmdlets that request user authentication, such as ...
🌐
ShellGeek
shellgeek.com › home › powershell tips › get current user name in powershell
Get Current User name in PowerShell - ShellGeek
April 14, 2024 - To get current user name in PowerShell, use whoami, GetCurrent() method of WindowsIdentity, $env or Get-WMIObject cmdlet in PowerShell.
🌐
Octopus Deploy
octopus.com › docs › projects › variables › username-password-account-variables
Username And Password Account Variables | Documentation and Support
August 29, 2024 - Each of the above properties can be referenced in any of the supported scripting languages such as PowerShell and Bash. Although it’s possible to reference the Password of the Username and password account in a script, writing them out to stdout will result in Octopus masking the values as they are considered sensitive. ... # For an account with a variable name of 'username password account' # Using $OctopusParameters Write-Host 'UsernamePasswordAccount.Id=' $OctopusParameters["username password account"] Write-Host 'UsernamePassword.Username=' $OctopusParameters["username password account.U