-1

I am currently working on a batch script and would like to add new paths to the system environment variables via registry command. Here is my current code:

set newPath=%path%;%NODE_MODULES_DIR%;%NODE_MODULES_DIR%\electron\dist
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%newPath%" /f

call logoff

I do a reg add with new paths. And as I understand, since changes will not take effect until after a logout/restart, I added call logoff to force user to log out.

Problem is when I log back in, I still can't see my new paths under system environment variables.

I do not want to use the setx command because of the 1024 limitation.

Can someone help? Thanks!!

kzaiwo
  • 1,558
  • 1
  • 16
  • 45

1 Answers1

-1

Found what's wrong already.

Turns out that the HKEY_LOCAL_MACHINE path is incorrect.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment

should be

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

(Session Manager instead of Sessions Manager)

And should've used REG_SZ instead of REG_EXPAND_SZ

kzaiwo
  • 1,558
  • 1
  • 16
  • 45
  • For the future: Study the few [batch files posted by me](https://stackoverflow.com/search?q=user%3A3074564+%5Bbatch-file%5D+setx+path) which modify system and/or user PATH quite safely, but still not 100% safe for all possible use cases. A folder path inside `PATH` with a semicolon in folder path and therefore enclosed by exception in double quotes is most likely not handled correct by my batch files. That is an extremely tricky to handle use case which is very rare. – Mofi Nov 27 '19 at 07:08
  • Read carefully [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) with a batch file coded to quite safely update __system__ `PATH` environment variable. But read also the other few answers written by me and listed on page opened by clicking on the link in above comment to understand the problematic of your approach completely. – Mofi Nov 27 '19 at 07:20