Setting or modifying a (system wide) environment variable in cmd.exe

Solution 1:

The Old School method of directly manipulating registry variables with the reg command was on the money. Here's how you do it:

reg add HKCU\Environment /v PATH /d "%addonpath%;%path%" /f

Throw that into a one line script called apath.bat that looks like this:

@echo off
reg add HKCU\Environment /v PATH /d "%~dp0;%path%" /f

Then, all you need to provide is the path of the new directory you're adding when calling the script and you're dialed in:

e.g: apath.bat %addonpath%

Although Hinch is right. The best way to do it if you're using Vista or above is to use the SETX command which is designed to allow us to propagate environment variables without the risk of directly manipulating the registry with with the reg command that could save you your machine if you manipulate ENV variables enough to use it on the fly.

Solution 2:

You could use setx.

User variable:

SETX PATH "%PATH%;C:\MyDir"

System variable:

SETX PATH "%PATH%;C:\MyDir" /M