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.

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.

share|improve this question
    
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 Feb 16 '12 at 15:53
    
@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 Feb 16 '12 at 15:56
1  
I 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 Feb 16 '12 at 21:32
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 Feb 16 '12 at 22:17
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 Feb 17 '12 at 15:31
show 19 more comments

1 Answer

up vote 3 down vote accepted

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"
set VALUE_NAME=Path

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.
)
share|improve this answer
2  
Be 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 Feb 20 '12 at 2:15
    
The new script doesn't work as written if the path contains brackets, e.g., any references to Program Files (x86). –  Harry Johnston Feb 20 '12 at 21:14
add comment

Your Answer

 
discard

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.