Using REG to add a variable to User variable Path

sValentin 81 Reputation points
2022-07-13T09:38:12.543+00:00

I want to use REG to add a value to User variable Path (not System path) that in the future may have to be linked to a new folder, and for that reason I'm also adding a User variable ("MyVariable" for this example) which in the future if needed will be changed to a new folder. This way will be easier then removing the old value from Path and adding the new one. But the problem that I have is that when I run the following command it will copy the values from System variable Path and then add the value that I want to User variable Path, and it doesn't copy the values from User variable Path that I was thinking it will do. The reason for adding the values is that the program that I'm creating needs to have a folder in User or System Path, and as User doesn't need administrator rights, I'm using that one. What do I need to change so that it will just add the value that I want to User variable Path and stop it from copying the values from System Path?

reg add "HKEY_CURRENT_USER\Environment" /v PATH /t REG_EXPAND_SZ /d "%path%;%MyVariable%\folder\location" /f  

//Edit: I ended up switching to use RegOpenKeyExA, RegQueryValueExA, RegCreateKeyEx, RegSetValueExA, and I used SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)TEXT("Environment")); to get past the need for a system restart/logout.

Windows for business Windows Client for IT Pros User experience Other
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,681 Reputation points
    2022-07-13T10:10:35.153+00:00

    Changing User or System Environment variables is done via registry + WM_SETTINGCHANGE,

    like :

                        HKEY hKey;  
                        DWORD dwResult = 0;  
                        LONG nResult = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Environment"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwResult);  
                        if (nResult == ERROR_SUCCESS)  
                        {  
                            TCHAR wsValue[] = TEXT("Test");  
                            RegSetValueEx(hKey, TEXT("MyVariable"), 0, REG_SZ, (LPBYTE)wsValue, (_tcslen(wsValue) + 1) * sizeof(TCHAR));  
                        }  
                        if (hKey)  
                            RegCloseKey(hKey);  
                        SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)TEXT("Environment"));  
    

  2. RLWA32 49,536 Reputation points
    2022-07-13T16:31:05.463+00:00

    Use the following commands in a batch file. The batch file will create the user environment variable and the desired path string. A logout/login is needed to see the effect of the changes.

    for /f "usebackq tokens=3" %%i in (`reg.exe query HKCU\Environment /v Path`) do set oldpath=%%i  
    reg add HKCU\Environment /v MyVariable /t REG_SZ /d TestFolder /f  
    reg add HKCU\Environment /v Path /t REG_EXPAND_SZ /d %oldpath%;^%%MyVariable^%%\folder\location /f  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.