15

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.

I'd be grateful if someone showed me a non-GUI way to modify (that is: to add a new directory to) the %PATH% variable.

The change should be be permanent, not just for duration of the cmd session.

1
  • 1
    i think one problem you'll find is that most changes to the permanent, system-wide environment variables require a logout/login (or reboot) for the user's session to use the modified values. Nov 4, 2009 at 21:15

8 Answers 8

8

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.

3
  • Does this command requires a restart?
    – Juzer Ali
    Dec 17, 2012 at 13:28
  • No, but you have to use a new (cmd)process.
    – mike
    Jan 22, 2014 at 16:15
  • 1
    For me, it does need restart? why? I am on Windows XP, sp3. Feb 1, 2015 at 13:07
17

You could use setx.

User variable:

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

System variable:

SETX PATH "%PATH%;C:\MyDir" /M
5
  • I believe SETX is available since Vista, but I am on XP. Nov 4, 2009 at 20:40
  • you might be able to get away with copying setx.exe from a Vista machine. probably won't work, but it might. Nov 4, 2009 at 20:52
  • 2
    SETX is part of the Resource Tools for Windows Server 2003. You can just add it to your system32 directory, or any other added to you path (chicken-egg!).
    – paradroid
    Oct 4, 2010 at 14:58
  • I need the /M part so I can modify the system variable, not the user one. Thanks! Feb 25, 2012 at 11:31
  • Both specified commands are wrong. Why? Windows actually maintains two PATH values: system-wide and per-user. When you type echo %PATH%, you see them combined together, like <system path>;<user path>. Thus, first command will force current items of system path appear in the user path (which is not so bad), while second command will cause current items of user path to appear in the system path (which is unacceptable).
    – Sasha
    Jun 27, 2015 at 15:27
1

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.

2
  • As far as I know that would only influence subsequent invocations of cmd.exe (without the /d flag), but not the entire system or other (subsequently invoked) processes. Nov 4, 2009 at 20:42
  • That is correct. I find that an advantage, means you can do more to it without worrying about different processes not knowing what to do. It WILL, however, work in any interactive prompt, which is always nice :P
    – Phoshi
    Nov 4, 2009 at 20:46
1

If you don't want to use the GUI (as in Control Panel, System, Advanced, Environment Variables, PATH) you can probably use REG to set HKCU\Environment\PATH.

  • update %PATH%
  • REG ADD HKCU\Environment /v PATH /t REG\_EXPAND\_SZ /d "%PATH%" /f

The /f forces overwriting of the existing value so you don't have to interactively answer the question.

Edit: %PATH% needs to be quoted.

Edit: It's also worth noting that this probably requires a reboot or re-login before it takes effect. While changing it in the GUI takes effect immediately (for new cmd.exe sessions).

Reference: http://support.microsoft.com/kb/104011

As noted in the reference, if you wanted to write some code, you could send WM_SETTINGCHANGE and that should avoid the login/logout requirement.

5
  • that would be very useful. But, if I query on that variable, it doesn't return anything.
    – PA.
    Nov 4, 2009 at 21:23
  • Hrm. On XP Pro SP3 English I get my PATH with: REG QUERY HKCU\Environment /v PATH.
    – opello
    Nov 4, 2009 at 21:28
  • it's a user-specific path, not the system-wide path. not sure where that one lives. they can be set to the same thing but usually contain separate things (and your working path is a merging of the two). Nov 4, 2009 at 21:45
  • Right, it's in HKCU. The Microsoft KB article linked has both registry locations.
    – opello
    Nov 4, 2009 at 22:13
  • 1
    This is almost what I am looking for... if there was a way to also send the WM_SETTINGCHANGE from cmd.exe. Aug 24, 2010 at 23:07
0

It's easy to change the path in the current cmd.exe process:

PATH c:\MyNewDirectory;%PATH%

You can always do HELP PATH for help on the PATH command.

3
  • 1
    Isn't this only good for the session?
    – JL.
    Nov 4, 2009 at 20:17
  • yes, this does not persist across sessions.
    – user1931
    Nov 4, 2009 at 20:18
  • JL is right and I was looking for a permanent solution. I have edited my question accordingly. Nov 4, 2009 at 20:19
0

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?

0

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)

0

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 :-)

https://autohotkey.com/board/topic/63210-modify-system-path-gui/

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: https://stackoverflow.com/questions/3304463/how-do-i-modify-the-path-environment-variable-when-running-an-inno-setup-install

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .