Search code examples
windowscmdpath

How to update PATH variable permanently from Windows command line?


If I execute set PATH=%PATH%;C:\\Something\\bin from the command line (cmd.exe) and then execute echo %PATH% I see this string added to the PATH. If I close and open the command line, that new string is not in PATH.

How can I update PATH permanently from the command line for all processes in the future, not just for the current process?

I don't want to do this by going to System Properties → Advanced → Environment variables and update PATH there.

This command must be executed from a Java application (please see my other question).


Solution

  • The documentation on how to do this can be found on MSDN. The key extract is this:

    To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.

    Note that your application will need elevated admin rights in order to be able to modify this key.

    You indicate in the comments that you would be happy to modify just the per-user environment. Do this by editing the values in HKEY_CURRENT_USER\Environment. As before, make sure that you broadcast a WM_SETTINGCHANGE message.

    You should be able to do this from your Java application easily enough using the JNI registry classes.