There could be a situation where we want to add new path to the Path environment variable using batch script. Let’s say you have a directory build/commands and you want to add this path to the Windows Path environment variable.

windows environment path

Let’s then create small batch script setpath.bat which will do few things:

  • be able to execute with UAC privilege elevation
  • read the current directory path so that you don’t have to worry about specifying full path
  • add specified path to the end of PATH configuration
  • check, if at the end of PATH configuration string there is semicolon or not and add new path

The batch script code may look like this:

SET newpath=build\commands
pushd %~dp0
IF "%path:~-1%"==";" (SETX PATH "%path%%~dp0%newpath%" /m) else (SETX PATH "%path%;%~dp0%newpath%" /m)
popd %~dp0

To run it simply execute from command line: elevate setpath.bat. This should work in Windows XP and up.

If your execution path is, for example, C:\Projects\Test, then running above script would add following path to the PATH environment: C:\Projects\Test\build\commands.

Note: do not forget to restart Windows in order to apply changes. At least that’s what’s working for me.


You can leave a response, or trackback from your own site.

Before you add comment see for rules.

Leave a Reply

Your email address will not be published. Required fields are marked *