How can I permanently append an entry into the system's PATH variable, via command line?



What I need to do:

  • Append a folder to the %PATH% environment variable at the SYSTEM level.
  • Make the change permanent.

How I need to do it:

  • Using the command prompt, or another method by which all necessary commands can be written to a .BAT file.
  • Using only tools which would be available on a bare install of Windows XP SP3, without Internet connectivity.
  • I'd rather run the script locally, but I do also have remote access to the target systems. Bear in mind though, that I cannot presume any non-default services (i.e.: Remote Registry) are enabled on the systems.

Systems the script needs to work on:

  • Windows XP SP3
  • Windows Server 2003 SP2
  • Windows 7 SP1
  • Windows Server 2008 R2 SP1

I'm fairly familiar with the SET command, but I'm also aware that it will generally overwrite the existing variable instead of append to it. That is not acceptable. Is there another tool (or option for SET, which I'm unaware of) that will append to the variable instead? Or, do I need to put a work-around in the script that includes temporarily copying the existing variable to another variable or text file?

Also, it's my understanding that SET will not permanently alter the variable. I've seen mention of SETX, but that does not seem to come built-in to Windows XP SP3 (or, at least, it doesn't appear to be available on the system I'm working on). Is there another way to make the change permanent, via registry edit or something?

I've done some looking around and have learned a good bit from here about setting environment variables in Windows. However, I haven't yet found an exact duplicate question that will suit my needs. If there is one, please do let me know.


Posted 2012-02-16T15:40:04.237

Reputation: 11 686

Setx does not come with XP. It is in one or more of the resource kits, but I forget which offhand. To make permanent environment variable changes at the command line it is definitely the easiest way. You can make the change in the registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment) but registry changes from the command line are no piece of cake either. If I were you, then I would write a VBScript to do this. – EBGreen – 2012-02-16T15:53:11.107

@EBGreen I'm a bit familiar with using REG to do registry changes. If you'd like to post that as an answer, with suggestions on how to retain the existing path, I'd at least give it an up-vote. – Iszi – 2012-02-16T15:56:16.753

To be honest I have not worked in this environment (pardon the pun) in years. So to post an answer that I would be satisfied with I would have to work it all out again. If this were my problem to solve, I would just use setx from my machine to change whatever machines need to be changed (you know it works remotely too right). – EBGreen – 2012-02-16T16:30:28.640

I lied (see having to relearn this old crap anyway). It does not work remotely so I would combine it with PSExec to set the path on remote machines. – EBGreen – 2012-02-16T16:35:19.510

@EBGreen This doesn't have to be done remotely (unless you count RDP). Did I say something that implied it did? – Iszi – 2012-02-16T16:46:25.940

No, I'm just saying that is how I would do it (in my environment at least) because I don't have the time right now to suss out an answer that fits the criteria that you did express. – EBGreen – 2012-02-16T16:51:12.280

@lszi Being able to do it remotely, were it possible, would get around the limitation of "Using only tools which would be available on a bare install of Windows XP SP3". – Myrddin Emrys – 2012-02-16T16:57:23.587

@MyrddinEmrys Good point. I'd rather a solution that works locally, but remote access isn't a problem either. Just keep in mind that, per the stipulations above, you can only presume default services are running on the target systems. While stuff like Remote Registry may be enabled on some of these systems, I wouldn't trust it to be available on all. – Iszi – 2012-02-16T17:03:37.167

I'm not quite sure I understand though; fetching a file (such as setx) can be done via the command line. Wouldn't that be a viable solution? Ahh, I see, you can't guarantee Internet access. But you're sending them a script... why can't you send them setx as well? – Myrddin Emrys – 2012-02-16T17:37:25.597

@MyrddinEmrys I'm not going to be installing or running any additional tools or applications (other than the one for which this is needed) on these systems. – Iszi – 2012-02-16T17:58:42.357

Would it be an option to use an installer? They are pretty easy to use unless you have exotic requirements. I suggest NSIS or Inno Setup – Nifle – 2012-02-16T19:36:34.860

@Nifle Since the specifications were to keep all commands in .BAT and not have external utilities running locally on the targets, I would say no. – Iszi – 2012-02-16T19:57:04.507

It would be interesting to know why the .BAT only requirement exists. If it's to guarantee that no other applications needs to be installed an installer would work. If the goal is to use only local resources powershell should work. – Nifle – 2012-02-16T20:03:19.430

Not on XP or Vista it wouldn't, since you would have to install it first :) – EBGreen – 2012-02-16T20:11:18.850

@Nifle The .BAT only requirement is pretty much because that's the only scripting language I speak fluently-ish and it comes built in to the system (no third-party software, i.e. installers, required). And, as EBGreen mentions, PowerShell does not come built-in to Windows before 2008/7. – Iszi – 2012-02-16T20:13:41.070

As EBGreen suggests, it is theoretically possible to write a command script (aka .BAT file) which uses reg.exe to do what you want. Personally I wouldn't trust it; note in particular that if the existing PATH contains any Unicode characters they'll be lost. It would be a lot safer to write an executable instead, or build this functionality into the executable that your batch file is installing. – Harry Johnston – 2012-02-16T21:27:28.217

1I think, per EBGreen's suggestion, I've figured out how I need to do it. Now I just wish there was a way to force the registry refresh (and push to environment variables) without rebooting or reloading Explorer? – Iszi – 2012-02-16T21:32:27.547

Pretty sure that there is no way to avoid a reboot. – EBGreen – 2012-02-16T22:16:04.787

1@EBGreen Yeah, that's what it looks like. By the way, if you have some time to figure out the syntax, could you please post an actual answer? I'd rather this not turn into a self-answered question. – Iszi – 2012-02-16T22:17:42.450

sorry, have a fire we are putting out here at work. – EBGreen – 2012-02-16T22:45:17.090

@EBGreen No problem. I'll give you a day or two if you like. – Iszi – 2012-02-17T00:45:57.610

That's very generous of you. Having been told the registry key that needs to be changed, and having admitted that you have experience in the use of Reg, have you made any effort to figure it out on your own? – EBGreen – 2012-02-17T13:53:08.847

1@lszi, you should answer your own question. A good answer to a good question are both valuable, even when you answer it yourself. – Myrddin Emrys – 2012-02-17T15:31:48.527

Just to clarify, I do not answer questions here for rep. I do it as a diversion to entertain myself while I work. So if I find the question of particular interest, or it is something that I expect to find personally useful then I will work out a detailed answer. Otherwise I generally just try to guide people in the direction of solving it for themselves.\ – EBGreen – 2012-02-17T15:46:12.883



The following adds 'C:\bin' to your path and then saves the new path into the Registry:

set path=%path%;C:\bin
reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d %path% /f

I only tested this on XP SP3, but it should work on newer version as well.

I guess a new user who logs on before the machine reboots may not get the new path.

Harry is right with his comment about %SystemRoot%, if you want to keep these, you need to pull the old value for path from the registry first:

@echo OFF

set KEY_NAME="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
  set ValueName=%%A
  set ValueValue=%%C

if defined ValueName (

  set newPath=%ValueValue%;C:\bin

  reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d %newPath% /f

  set path=%path%;C:\bin

) else (
    @echo %KEY_NAME%\%VALUE_NAME% not found.

Peter Hahndorf

Posted 2012-02-16T15:40:04.237

Reputation: 10 677

2Be aware that this will change some of the existing PATH entries by expanding environment variable references. For example, the standard entry %SystemRoot%\system32 will be replaced with c:\windows\system32. This particular example doesn't matter because SystemRoot is a constant, but if someone has set up a PATH with a reference to a environment variable that changes you'll break it. (I do this on some of my computers to point to the bin directory in the current Sun JDK.) – Harry Johnston – 2012-02-20T02:15:19.590

The new script doesn't work as written if the path contains brackets, e.g., any references to Program Files (x86). – Harry Johnston – 2012-02-20T21:14:50.273


set path=%path%;C:\bin
reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d ^%path^% /f


Posted 2012-02-16T15:40:04.237

Reputation: 1

2While bare commands can be useful, we expect a little more work in our questions/answers. Please explain how this answers the OPs question. – Ƭᴇcʜιᴇ007 – 2015-05-12T21:40:01.983