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

13

8

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.

René Nyffenegger

Posted 2009-11-04T20:10:35.080

Reputation: 1 862

1i 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 – 2009-11-04T21:15:29.160

Answers

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.

user51259

Posted 2009-11-04T20:10:35.080

Reputation: 96

1For me, it does need restart? why? I am on Windows XP, sp3. – Changwang Zhang – 2015-02-01T13:07:29.817

Does this command requires a restart? – Juzer Ali – 2012-12-17T13:28:40.507

No, but you have to use a new (cmd)process. – mike – 2014-01-22T16:15:09.087

15

You could use setx.

User variable:

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

System variable:

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

user16765

Posted 2009-11-04T20:10:35.080

Reputation:

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 – 2015-06-27T15:27:05.607

2SETX 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 – 2010-10-04T14:58:24.630

I need the /M part so I can modify the system variable, not the user one. Thanks! – Andrei Sfat – 2012-02-25T11:31:11.313

I believe SETX is available since Vista, but I am on XP. – René Nyffenegger – 2009-11-04T20:40:02.117

you might be able to get away with copying setx.exe from a Vista machine. probably won't work, but it might. – quack quixote – 2009-11-04T20:52:01.297

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.

Phoshi

Posted 2009-11-04T20:10:35.080

Reputation: 22 001

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 – 2009-11-04T20:42:44.653

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 – 2009-11-04T20:46:39.797

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.

opello

Posted 2009-11-04T20:10:35.080

Reputation: 696

1This is almost what I am looking for... if there was a way to also send the WM_SETTINGCHANGE from cmd.exe. – René Nyffenegger – 2010-08-24T23:07:32.137

that would be very useful. But, if I query on that variable, it doesn't return anything. – PA. – 2009-11-04T21:23:40.453

Hrm. On XP Pro SP3 English I get my PATH with: REG QUERY HKCU\Environment /v PATH. – opello – 2009-11-04T21:28:09.540

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 – 2009-11-04T21:45:25.543

Right, it's in HKCU. The Microsoft KB article linked has both registry locations. – opello – 2009-11-04T22:13:04.097

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

Justin Goldberg

Posted 2009-11-04T20:10:35.080

Reputation: 434

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)

Skatterbrainz

Posted 2009-11-04T20:10:35.080

Reputation: 817

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.

jdigital

Posted 2009-11-04T20:10:35.080

Reputation: 861

1Isn't this only good for the session? – JL. – 2009-11-04T20:17:23.160

yes, this does not persist across sessions. – John T – 2009-11-04T20:18:32.193

JL is right and I was looking for a permanent solution. I have edited my question accordingly. – René Nyffenegger – 2009-11-04T20:19:16.917

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?

quack quixote

Posted 2009-11-04T20:10:35.080

Reputation: 37 382