2
I don't understand this. So currently my system environment variable named "PSModulePath" looks like this:
%ProgramFiles%\WindowsPowerShell\Modules;%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules
Now observe the following PowerShell script:
$envarname = "PSModulePath"
$envar = (get-item env:$envarname).Value
[Environment]::SetEnvironmentVariable($envarname, $envar + ";C:\Expedited", "Machine")
All it should be doing is adding the path "C:\Expedited" to the PSModulesPath environment variable, right? Well, after running this script as administrator, the PSModulePath environment variable changes into this:
C:\Users\Username\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Expedited
Notice how:
- There were originally two paths, each of which contained percentage signs (variables) in the original, but afterward they all changed directly into hard-coded paths.
- The "C:\Users\Username\Documents\WindowsPowerShell\Modules" path sprung out of nowhere (it wasn't in the original!)
I don't have any idea why either of these two things happened. When adding a path to this variable, I would like to keep it as close to the original as possible, not make all these other changes. Is there any way to preserve the percentage signs that were lost? How do I edit this environment variable correctly from within PowerShell?
"C:\Users\Username\Documents\WindowsPowerShell\Modules" - This would only exist in the user's PATH variable. It appears you are modifying the user's PATH variable instead of the system's PATH variable hence the difference. This question has an answer that explains the differences.
– Ramhound – 2018-07-16T19:55:40.3171@PimpJuiceIT, just open System Properties\Advanced\Environment Variables and take a look in there yourself. If you use the actual UI, or even check the corresponding registry entry, you can see the percentage signs in the text string. The problem is that I cannot get the string with the percentage signs using PowerShell for some reason; they always get converted to hard-coded paths and I don't know why. – jippyjoe4 – 2018-07-17T17:06:57.250