title: Batch: How to Prepend the PATH Environment Variable
body: <p>In Windows, the registry entry <code>HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path</code> contains the contents of the <code>PATH</code> environment variable.  In my case, the <code>PATH</code> environment variable is of type <code>REG_EXPAND_SZ</code>, with the contents:</p>
  
  <pre><code>%SOME_PATH%\bin;C:\Windows
  </code></pre>
  
  <p>In addition, I have a user environment variable named <code>PATH</code>, which is defined as:</p>
  
  <pre><code>%PATH%;C:\Users\Me\Bin
  </code></pre>
  
  <p>If I type <code>ECHO %PATH%</code> on the command line, the output is:</p>
  
  <pre><code>C:\Some\Path\bin;C:\Windows;C:\Users\Me\Bin
  </code></pre>
  
  <p>OK, now for the desired result given the above scenario.  I want to permanently prepend the machine's <code>PATH</code> 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:</p>
  
  <pre><code>C:\My\Path;%SOME_PATH%\bin;C:\Windows
  </code></pre>
  
  <p>Searching the web on how to do this yields the following approaches:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>Is there a way to accomplish what I am trying to do from within a Batch file?</p>