Two problems. If you want variable/expression substitution to take place in a string, you need to use quotation-marks, not apostrophes, to enclose the string. In this case, you also need to add some parenthesis to denote an expression within the string.
"\\SERVER\PATH\TO FILES\$([Environment]::UserName)\Normal\"
Answer from OldFart on Stack OverflowVideos
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.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.
I'm running a script from a specific location and I want the script to call on itself at the end. What I mean by this is the very last line of this script is
c:\Users\Username\Documents\MFACheck.ps1
However, I want this to work for anyone who uses the script. When I have my username in the the place of the "Username" in the path above, the script acts as I want it to. Is there a variable that can call on the current logged in user's username to place in that spot? Note that PowerShell needs to be run as an admin, but PowerShell will be used from a non-admin account. I don't know if that will change how the variable I'm looking for can be called. Thanks in advance!
Edit: I found something that works! Using some information from this link: https://adamtheautomator.com/how-to-get-the-current-user-logged-on-with-powershell-all-the-ways/ As well as with help from u/monkeybutt227
I am now using Invoke-Expression -Command "c:\Users$(((Get-WMIObject -ClassName Win32_ComputerSystem).Username).Split('')[1])\Documents\MFACheck.ps1"
and it's working!
It's possible.
Please try $env:USERNAME instead of a %username%. This way you get the username of the user that is running the PowerShell script.
Hope this helps.
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Andreas Baumgarten
If you are looking for a full list of environment variables and it's content you can try this:
dir env:
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Andreas Baumgarten
Hello Powershellers
In my current environment, there are several users who’s profile folder name is different than their username due to AD changes. So I’m looking for a way to find the profile path of the logged in user and I can’t necessarily rely on the username. The other twist is that the power shell script is run by third party software and I’m not sure which service it runs as because $env:Userprofile variable doesn’t work when using the software. Any other ideas on how I can do this? My end goal is to copy a shortcut that is under a users profile to the startup folder.
# this will do
Set-Location -path "c:\users\$env:username\desktop"
# alternative syntax
cd "c:\users\$env:username\desktop"
In powershell, environment variables are accessed via $ENV:Variable.
For example:
Set-Location -Path "C:\Users\$ENV:UserName"
Alternatively, you can use the C# API:
Set-Location -Path [Environment]::ExpandEnvironmentVariables("C:\Users\%Username%")