I found it:

[Environment]::UserName
$Env:UserName

There is also:

$Env:UserDomain
$Env:ComputerName
Answer from Thomas Bratt on Stack Overflow
🌐
Spiceworks
community.spiceworks.com › programming & development
Powershell: Getting current logged on user - Programming & Development - Spiceworks Community
November 27, 2013 - Ok, this is a little more difficult than the title makes it out to be. I know $env:username will get me the user. Unfortunately, that’s not the user I want to get when I’m in an elevated ps window. Basically I am usin…
🌐
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?

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

🌐
PowerShell Forums
forums.powershell.org › powershell help
Need to find the logged on user on windows 10 machine - PowerShell Help - PowerShell Forums
March 29, 2024 - Hi Team, i am using the below command to retreive the logged on user on win10 machine $userInfo = Get-WmiObject -Class Win32_ComputerSystem $($userInfo.UserName) The problem with this command is that it only retreives…
🌐
FOG Project
forums.fogproject.org › topic › 10940 › windows-powershell-username-computername › 4
Windows (Powershell) / Username=Computername | FOG Project
October 17, 2017 - When username and computername differs, everything is fine. ... Worked for me when I ran it locally on a Windows 10 Enterprise x64 computer. Will run as snapin soon to test. ... $Hostname = (Get-WmiObject -Class Win32_ComputerSystem).Name $Password = "password" | ConvertTo-SecureString -AsPlainText -Force New-LocalUser -Name $Hostname -Password $Password -Description "Local user." | Add-LocalGroupMember -Group "Users" ... Having a Powershell-script, that does more, for example, set ip-addresses, active office/windows, sets the correct time, etc.
Find elsewhere
🌐
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.
🌐
YouTube
youtube.com › watch
How to get User Name Current Path and Date with PowerShell - YouTube
In this video, we will learn how to get User Name Current Path, and Date with PowerShell. Please Subscribe to my Channel. Code : https://github.com/asimcode2...
Published   October 31, 2021
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.security › get-credential
Get-Credential (Microsoft.PowerShell.Security) - PowerShell | Microsoft Learn
You can use the object as input to cmdlets that request user authentication, such as those with a Credential parameter. However, some providers that are installed with PowerShell do not support the Credential parameter. $c = Get-Credential -Credential User01 $c.Username User01
🌐
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 - Whoami command: It is an executable file that can be used to find the username in the System32 folder. Query command: It can be used to list all currently logged-on user names on the remote computer via the Remote Desktop Protocol. Now, you should have an overall understanding of the PowerShell get users name cmdlets.
🌐
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 - Sometimes, you might need more ... in classic versions of PowerShell (PowerShell 5.1 and earlier), you can use the Get-WmiObject cmdlet....
Top answer
1 of 1
1
Get Current User Name in PowerShell · You can easily get the current username in Windows PowerShell using whoami to return domain\username as below · There are also many ways to get the current logged on username in Windows PowerShell as the following: · Using System.Security.Principal.WindowsIdentity · Using $env · Using Win32_ComputerSystem · 1) Get current logged-on username in Windows PowerShell · [System.Security.Principal.WindowsIdentity]::GetCurrent().Name · This example helps you to get the current user who runs the PowerShell domain\username. so if you are run the Powershell as a different user it will get the current user who runs the PowerShell, not the currently logged-on user to the machine. · Output · 2) Get current logged-on username without domain in Windows PowerShell · [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.split("\")[1] · Or · $env:UserName · This example helps you to get the current user who runs the PowerShell without domain. · 3) Get domain name in Windows PowerShell · [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.split("\")[0] · $env:UserDomain #not recommened · This example helps you to get only the current domain name. · 4) Get current logged-on username to machine in PowerShell · $userInfo=((Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username) -split '\\' ) $userInfo[0]+"\"+$userInfo[1] · This example helps you to get the logged-in user info like Domain Name $userInfo[0] and User Name $userInfo[1] or domain\username $userInfo[0]+"\"+$userInfo[1]. but if you are running the Powershell as a different user it will not get the current user who runs the PowerShell, it only gets the currently logged-on user to the machine. · See Also · Get all Groups a user is a member of Using PowerShell · PowerShell Get-Date in Short format
🌐
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 "$env:USERPROFILE\AppData\Local\Google\Chrome\User Data\Default\Bookmarks" -Destination "C:\Temp$($env:USERNAME)\chrome"
🌐
PowerShell Gallery
powershellgallery.com › packages › New-Username › 1.0 › Content › New-Username.ps1
PowerShell Gallery | New-Username.ps1 1.0
New-Username · New-Username.ps1 · Contact Us · Terms of Use · Gallery Status · Feedback · © 2026 Microsoft Corporation
🌐
Serveracademy
community.serveracademy.com › powershell
How to find User Logon Name by User Display Name in PowerShell - PowerShell - Server Academy Community Forum
May 3, 2022 - I have multiple display name of users in a csv file what should I do if I want to find their samaccount name and email address and from ad using PowerShell. and also a outcome if PowerShell cant find the user. we can do …