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.