I found it:
[Environment]::UserName
$Env:UserName
There is also:
$Env:UserDomain
$Env:ComputerName
Answer from Thomas Bratt on Stack OverflowSo 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.exewould 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.
Videos
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)
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?