Home » Questions » Computers [ Ask a new question ]

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

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

I am looking for a convenient way to add and/or modify and/or delete an environment variable from the command line. Particularly, I find myself at times in situations when I have to add a few variables in cmd.exe.

Asked by: Guest | Views: 59
Total answers/comments: 5
Guest [Entry]

"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."
Guest [Entry]

You could use the HKEY_CURRENT_USER\Software\Microsoft\Command Processor\Autorun registry key to point at a batch file, to allow you to make semi-permanent changes without delving into arcane settings dialogues.
Guest [Entry]

"For truly permanent, system-wide changes, you really want to use the System control panel (aka My Computer -> Properties -> Advanced -> Environment Variables, for WinXP). The settings there affect your whole system, including GUI programs in the Explorer shell.

If you only need these changes in the cmd.exe shell, you can run a batchfile that sets them whenever you start a cmd.exe window. Phoshi's answer mentions the HKEY_CURRENT_USER\Software\Microsoft\Command Processor\Autorun, which seems like an excellent option -- easy to make small changes to, and rerun from the commandline if you need to. But this won't affect GUI windows or the Explorer shell.

I'm actually surprised that Sysinternals doesn't have a capable utility to do this. Maybe another of the PStools can do it?"
Guest [Entry]

Lot's of ways to do this. REG ADD is one, or REG IMPORT (using an exported .REG file from another computer). SETX /M is another. You could also push it out using Group Policy Preferences (the hands-down easiest way for large numbers of computers)
Guest [Entry]

"Another thought not mentioned here, create an autohotkey script that will launch the control panel and enter it for you. It works well if you're already an AHK user :-)


Also what about editing hklm/system/currentcontrolset001/control/session manager/environment: path key ? But the same key is under hklm/system/currentcontrolset002 and hklm/system/currentcontrolset. It appears that this might be correct, per here: stackoverflow.com/questions/3304463/how-do-i-modify-the-path-environment-variable-when-running-an-inno-setup-install"