2
votes

Windows 2008R2 Powershell v2.0

Original Path (as seen from Advanced System Settings/Environment Variables) is:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\

From Powershell I run:

[Environment]::SetEnvironmentVariable("PATH", "$($env:path;C:\Temp", "Machine")

or

[Environment]::SetEnvironmentVariable("PATH", "$($([Environment]::GetEnvironmentVariable('PATH', 'MACHINE')));C:\Temp", "Machine")

Now my path (as seen from Advanced System Settings/Environment Variables) is:

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Temp

Is there a way to retrieve the existing path WITHOUT evaluating it so that I can retain the existing environment variables embedded in the original path?

4

4 Answers

3
votes

Kind of medieval, but seems to work:

(((reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment") |
  select-string "\s+path\s+REG_EXPAND_SZ").line -split "    ")[3]
1
votes

I think the problem is how PS is getting the environment variable;

This S/O post might contain the answer for you (hopefully!):

PowerShell: Get 'tmp' environment variable raw value

0
votes

Try this (after converting to PS):

string originalPath = Environment.GetEnvironmentVariable("PATH");
string path = originalPath + ";" + "NEW_PATH_BIT";
Environment.SetEnvironmentVariable("PATH", path);
-1
votes

I'm not sure there is - I tried this:

PS C:\> $string1 = " World"
PS C:\> $string2 = "Hello$string1"
PS C:\> $string2
Hello World

to see if it was related to environment variables or something else.

I guess it's the way it's compiled on the command line. All variables are evaluated before the whole expression is evaluated. So the end results doesn't know the working gone into it.