change environment variable on remote Windows machine

ModdyFire picture ModdyFire · Oct 21, 2012 · Viewed 9.4k times · Source

I'm trying to set an environment variable on a remote machine.

setx /s machine var value

doesn't work - it produces no output and doesn't do anything.

psexec \\machine setx var value

says it changed the value successfully. However when I go to the machine and open cmd, it doesn't show it. It does appear in the computer->properties->advanced->environment variables. Also, when I run

psexec \\machine cmd /c set

it is written correctly.

I wrote a program called RefreshEnvironment.exe that basically does the following:

    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
    (LPARAM) L"Environment", SMTO_ABORTIFHUNG,
    5000, &dwReturnValue);

If I call it on local machine, it fixes the problem - I open cmd after it and get the correct environment. However, it doesn't work remotely. Calling

psexec \\machine RefreshEnvironment.exe

returns "Access is denied". I can only run it with explicitly writing the user and password:

psexec \\machine -user domain\username -p password RefreshEnvironment.exe

Very strange since it is the same domain,username and password on both machines; and it doesn't work - didn't say anything, but I still can't see the environment on the local machine.

Any idea will be welcome.

Answer

Tim Bailen picture Tim Bailen · May 18, 2017

Make sure that the "Remote Registry" service is running on the target computer when using the /s parameter. Could this be the cause of your issue? On Windows 7, I receive a "ERROR: The specified operation could not be completed" if that service is not running on the remote computer.