Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
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. –  quack quixote Nov 4 '09 at 21:15

7 Answers 7

up vote 5 down vote accepted

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.

share|improve this answer
Does this command requires a restart? –  Juzer Ali Dec 17 '12 at 13:28
No, but you have to use a new (cmd)process. –  mike Jan 22 at 16:15

You could use setx.

User variable:


System variable:

share|improve this answer
I believe SETX is available since Vista, but I am on XP. –  René Nyffenegger Nov 4 '09 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. –  quack quixote Nov 4 '09 at 20:52
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 '10 at 14:58
I need the /M part so I can modify the system variable, not the user one. Thanks! –  sfat Feb 25 '12 at 11:31

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.

share|improve this answer
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. –  René Nyffenegger Nov 4 '09 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 '09 at 20:46

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.

share|improve this answer
that would be very useful. But, if I query on that variable, it doesn't return anything. –  PA. Nov 4 '09 at 21:23
Hrm. On XP Pro SP3 English I get my PATH with: REG QUERY HKCU\Environment /v PATH. –  opello Nov 4 '09 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). –  quack quixote Nov 4 '09 at 21:45
Right, it's in HKCU. The Microsoft KB article linked has both registry locations. –  opello Nov 4 '09 at 22:13
This is almost what I am looking for... if there was a way to also send the WM_SETTINGCHANGE from cmd.exe. –  René Nyffenegger Aug 24 '10 at 23:07

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.

share|improve this answer
Isn't this only good for the session? –  JL. Nov 4 '09 at 20:17
yes, this does not persist across sessions. –  John T Nov 4 '09 at 20:18
JL is right and I was looking for a permanent solution. I have edited my question accordingly. –  René Nyffenegger Nov 4 '09 at 20:19

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?

share|improve this answer

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)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.