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 OverflowUsername variable in Powershell script
Using $env:USERNAME or other variables in a script.
Use the %username% variable with the PowerShell Set-Location Commandlet - Stack Overflow
How do I get the current username in Windows PowerShell? - Stack Overflow
Videos
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.
# 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%")
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!
Try using $env:username instead of %username%:
Get-childitem \server\$env:username\desktop\ -filter file.ini
Get-ChildItem -path \server\$env:username\desktop\ -filter file.ini
PowerShell handles environment variables slightly differently than batch files. Instead of %username% you will want to reference $env:username instead.
Get-ChildItem "\server\$env:username\desktop\" -filter 'file.ini'
Mind you, that will only search the current drive with the folder 'server' being in the root of the drive, so if your drive that you are working on is the C: drive, it will only check this path:
C:\server\<username>\desktop\file.ini
You may want to add the -recurse switch to make it check subfolders, or take action to modify the path as needed. If you were intending to look on a remote server for the file, you may have wanted to do this:
Get-ChildItem "\\server\c$\users\$env:username\desktop\" -filter 'file.ini'
The %UserProfile% variable is a special system-wide environment variable that is complete in and of itself.
It contains %SystemDrive%\Users\{username}
See this fantastic table that highlights the differences between variables in windows XP (NT5) and Windows Vista/7/8 (NT6).
Try
mkdir %userprofile%\AppData\Roaming\modinstaller\mods
Its value is the location of the current user's profile directory, in which is found that user's HKEY_CURRENT_USER (HKCU) registry hive (NTUSER).
I assume you mixed up the variables %USERPROFILE% and %USERNAME%.
By default, %USERPROFILE% and C:\Users\%USERNAME% point to the same location. Since this is not guaranteed to be true, using %USERPROFILE% is a more reliable approach.
In general, when debugging a command like
mkdir C:\Users\%USERPROFILE%\AppData\Roaming\modinstaller\recovery
your first step should be to prepend echo.
The command
echo mkdir C:\Users\%USERPROFILE%\AppData\Roaming\modinstaller\recovery
would have shown you the following:
mkdir C:\Users\C:\Users\Tristan\AppData\Roaming\modinstaller\recovery
which is clearly not what you want.
You can also query the value of %USERPROFILE% by executing
set USERPROFILE
To see all currently defined environment variables, execute
set
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.