Modifying "Path" Environment Variable

This took me a couple of frustrating days to get working, and I wasn’t able to find a direct example in the forums to accomplish this, so I thought I would share!

We needed to add Java’s bin directory to the Windows ‘Path’ Environment Variable. I tried several approaches, including creating a .reg file, creating a .bat file, and simply using the command regset - none of them worked 100%. I finally gave up on the more “elegant” approaches, and just used the command line command to edit the registry. The action command that I ended up with looks something like this:

dos reg add “HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment” /v Path /t REG_EXPAND_SZ /d “%path%;{program files folder}\Java\jre7\bin” /f

/v - Value that you want to add
/r - data type that you’re adding
/d - data that you are adding to the value
/f - forces the key to be added even if it already exists

I was a combination of the existing Path variable and relevance to build the data that I needed to add. If anyone has any other experience with something similar, please share.

Do you have the code you tried using regset? I’m guessing that there was a syntax error of some sort, because there is no reason that this would work and regset and/or regset64 would not.

Also, I believe there is a way to add to the PATH using a command instead of modifying the registry directly.


SETX /M PATH "%PATH%;C:\your path with spaces"

Thanks, jgstew. I had also looked at the setx command to accomplish this, but I had initially thought using regset would be simpler as I have had more experience with it versus setx (which I have no experience with :smile:).

The original method I tried used the regset action command. Here’s the code:
regset “[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]” “Path”="{program files folder as string & “\Java\jre7\bin;” & value “Path” of key “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment” of registry as string}"

The command evaluates with no issues, but does not update the registry value at all. On 64-bit machine using regset64, I see the same result of no modifications to the registry.

Side Note:

regset64 works on both 32bit and 64bit systems, it just disables WoW redirection if applicable.

Because this isn’t targeting the HKLM/Software key, regset and regset64 are equivalent on all systems.

Does this part work correctly on its own?

program files folder as string & "\Java\jre7\bin;" & value "Path" of key "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" of registry as string

Try the following ActionScript:

dos setx /M PATH "%PATH%;{program files folder as string & "\Java\jre7\bin"}"

Yes - the relevance does evaluate correctly in the fixlet debugger, and when incorporated in the action command, it does evaluate to the correct data value that I want to add.

Also, your setx command works as well! Thank you!

1 Like

To manipulate the PATH variable, I personally prefer PATHMAN.EXE (which was in, I believe, the 2003 Resource Kit tools). PATHMAN can distinguish between per-machine and per-user PATH variables, and avoids adding a duplicate entry if the specified folder is already in the path.

1 Like

Good to know. PathMan is referenced here:

1 Like

I came across this while trying to figure out my similar problem. I need to recreate three system variables, after deleting them for upgrading JRE. I need to place them in HKLM\SYSTEM\CurrentControlSet\Control\Session\Manager\Environment\

One of the three variables is a simple string that’s easy to add using regset…but two have lengthy variables…here’s one of them. I tried regset and setx /m _JAVA_OPTIONS and everything after that…I assume I have incorrect syntax. Anyone know what to do?

_JAVA_OPTIONS=-Xrunjvmhook -Xbootclasspath/a: "C:\Program Files (x86)\HP\\Sprinter\bin\java_shared\classes";"C:\Program Files (x86)\HP\Sprinter\bin\java_shared\classes\jasmine.jar"

You can / should use something like PATHMAN to add things to the PATH on the command line. There are other options as well.

Can you provide the regset command you tried?

Hi, thanks for the reply. Pathman - I will do some reading.
Here’s the regset I attempted:
regset “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment_JAVA_OPTIONS” “StringValue”=""-Xrunjvmhook -Xbootclasspath/a:“C:\Program Files (x86)\HP\Sprinter\bin\java_shared\classes”;“C:\Program Files (x86)\HP\Sprinter\\bin\java_shared\classes\jasmine.jar”"

With this failure, I thought maybe I need to just some of the variable…that everything after the semicolon needed to be appended to the variable in a second action, so I deleted everything after the semicolon, but that didn’t work either.