How can I permanently append an entry into the system's PATH variable, via command line?
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.
)