Use the SETX command (note the 'x' suffix) to set variables that persist after the cmd window has been closed.
For example, to set an env var "foo" with value of "bar":
setx foo bar /m
Though it's worth reading the 'notes' that are displayed if you print the usage (setx /?), in particular:
On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window.
On a remote system, variables created or modified by this tool will be available at the next logon session.
In PowerShell, the [Environment]::SetEnvironmentVariable command.
Answer from Vik David on Stack OverflowUse the SETX command (note the 'x' suffix) to set variables that persist after the cmd window has been closed.
For example, to set an env var "foo" with value of "bar":
setx foo bar /m
Though it's worth reading the 'notes' that are displayed if you print the usage (setx /?), in particular:
On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window.
On a remote system, variables created or modified by this tool will be available at the next logon session.
In PowerShell, the [Environment]::SetEnvironmentVariable command.
The MSDN documentation for environment variables tells you what to do:
To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.
You will of course need admin rights to do this. I know of no way to broadcast a windows message from Windows batch so you'll need to write a small program to do this.
Videos
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.
You could use setx.
User variable:
SETX PATH "%PATH%;C:\MyDir"
System variable:
SETX PATH "%PATH%;C:\MyDir" /M
This can also be achieved from a command prompt. The following example sets a variable at the user level:
SETX variable_name value
For machine level, elevation is required:
SETX variable_name value /m
Right click on Computer, Advanced system settings, select Advanced tab and click Environment variables.
Note: once you modify the environment variables, you will have to restart your applications, including CLI.