I am tweaking the PATH on several W2k3 servers in PowerShell to convert certain paths with blank spaces to their 8.3 equivalents. After several regex transformations, I run the following two commands:
# Set the path for this process
$env:PATH = $path
# Set the path for the Machine
[System.Environment]::SetEnvironmentVariable('PATH', $path,[System.EnvironmentVariableTarget]::Machine)
After running them, the path is changed in ways I didn't intend. %SystemRoot% is uniformly expanded to C:\Windows. I can't see where this signals the apocalypse, but I'd rather keep the %SystemRoot%, so I fiddled until I got %SystemRoot% to appear in the path again, but when I do the path no longer expands and no longer works. Echoing the path at the CLI returns an unexpanded string (this is wrong) and commands in the SystemRoot can no longer be found.
If I then add a null entry to the Path ";;", without altering any other text in the PATH, it begins to work correctly.
So my question is how to alter the path programmatically using PowerShell so as not to muck up variable expansion within the path?
$pathwhen you are setting it toPATH?%USERPROFILE%.