How can I handle the extension of the Windows PATH variable when installing from PowerShell?

I configure PATH on multiple W2k3 servers in PowerShell to convert specific paths with empty spaces to their 8.3 equivalents. After several regular expression conversions, 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 starting, the path changes in a way that I did not expect. % SystemRoot% is evenly expanded to C: \ Windows. I don’t see where the apocalypse signals, but I would prefer to save% SystemRoot%, so I played until I got% SystemRoot% to appear on the path again, but when I make this path it no longer expands and does not expand anymore . Repeating the path in the CLI returns an unextended string (this is incorrect), and the commands in SystemRoot can no longer be found.

If I then add an empty entry to the ";;" Path without changing any other text in the PATH, it will start working correctly.

So my question is how to programmatically change the path using PowerShell so as not to drown out the variable extension in the path?

+5
source share
1 answer

As far as I can tell, you cannot do this with a method [Environment]::SetEnvironmentVariable(), and you cannot do this with a registry provider. However, you can access the Path env var system in the registry using the class Microsoft.Win32.RegistryKey:

C: \ PS> $ key = [Microsoft.Win32.Registry] :: LocalMachine.OpenSubKey ('SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Environment', $ true)
C:\PS> $path = $key.GetValue('Path',$null,'DoNotExpandEnvironmentNames')
C:\PS> $path
...;%systemroot%\System32\WindowsPowerShell\v1.0\
C:\PS> $key.SetValue('Path', $path + ';%Windir%\Symbols', 'ExpandString')
C:\PS> $key.Dispose()

PATH , Path.

+6

All Articles