How to change the PATH variable specifically through the command line in Windows

I would like to create a .bat file that will add some line to the end of the value of the Windows PATH variable. A warning. I want this change to be final, not just for the current session.

Does anyone know how to do this? As much as possible, it should not depend on the version of Windows

+7
windows system
source share
2 answers

Sorry for the long answer, but a short answer to your question is not possible.

First of all, you need to understand how environment variables work. There are places in the registry such as HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and HKEY_CURRENT_USER\Environment where environment variables will be saved. At startup, the operating system reads these registry keys. Then one Windows process creates another Windows process. The parent process can provide the client process with any set of environment variables. If the parent process does not do this, the child process inherits the environment variables of the parent processes.

To update the environment variables for the running process with respect to WM_WININICHANGE or WM_SETTINGCHANGE . A Windows application can interpret these messages and reread the current environment variables from the registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and HKEY_CURRENT_USER\Environment . Thus, you can generally change the registry values ​​under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment or HKEY_CURRENT_USER\Environment and send

 SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment"); 

It would be much better to use SendMessageTimeout instead of SendMessage , but the idea will remain the same. The problem is that other processes do not have to wait for a message and do something. Most console applications do not have a message loop and do nothing if you send such messages.

Therefore, it is important to understand that there is a simple, no easy way to update the environment variables of all processes without restarting the computer. You should have a clear understanding of this and slightly reduce your question.

If you update the environment in the registry and send SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment") , then the new processed created by Explorer.exe will have new environment variables, but cmd.exe will not do this.

If you want to update the environment variables of the current cmd.exe inside a batch run, you can do the following: you can create a new CMD file, for example t.cmd, in the% TEMP% directory, write in the file SET PATH=%PATH%;C:\BlaBla , and then use call %TEMP%\t.cmd and dell %TEMP%\t.cmd to update the environment variables of the current cmd.exe .

To be precise, there are more places that are used to create environment variables for newly created processes. These are key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths and %SystemRoot%\System32\autoexec.nt . One will be used for processes created by ShellExecute and ShellExecuteEx (for example, Explorer.exe), and the other for console applications.

+18
source share

If you only need new process instances, and you really want this done through a batch file, then setx is what you are looking for.

  • /M will change PATH to HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER .
    those. system variable, not user variable.
    Example: SETX /M PATH "%PATH%;C:\your path with spaces"

If you want to directly change the environment variable for current processes, well, yes, it is complex and apparently not recommended :

 Altering the environment variables of a child process during process creation is the only way one process can directly change the environment variables of another process. A process can never directly change the environment variables of another process that is not a child of that process. 

Otherwise, as Oleg says, it’s best to programmatically change the registry and send WM_SETTINGCHANGE and hope that the applications are good enough to pick it up.

+4
source share

All Articles