In Windows, the registry entry HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path
contains the contents of the PATH
environment variable. In my case, the PATH
environment variable is of type REG_EXPAND_SZ
, with the contents:
%SOME_PATH%\bin;C:\Windows
In addition, I have a user environment variable named PATH
, which is defined as:
%PATH%;C:\Users\Me\Bin
If I type ECHO %PATH%
on the command line, the output is:
C:\Some\Path\bin;C:\Windows;C:\Users\Me\Bin
OK, now for the desired result given the above scenario. I want to permanently prepend the machine's PATH
variable from within a batch script. After the change, I want all newly opened command windows to pick up my change without the need to log off or restart, and the aforementioned registry value should be changed to:
C:\My\Path;%SOME_PATH%\bin;C:\Windows
Searching the web on how to do this yields the following approaches:
1) SETX Path "C:\My\Path;%Path%" /m
Result: C:\My\Path;C:\Some\Path\bin;C:\Windows;C:\Users\Me\Bin
The first problem with this is that it expands the %SOME_PATH% environment
variable to it's corresponding value. The second is that %Path% expands
to include the user's Path variable.
2) REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"^
/v Path /t REG_EXPAND_SZ /d "C:\My\Path;%%B" /f
Result: C:\My\Path;%SOME_PATH%\bin;C:\Windows
The registry value ends up being correct, but I have to log off or
restart to pick up the changes.
Is there a way to accomplish what I am trying to do from within a Batch file?