2

Is there a way to set environment variables for explorer.exe that aren't there when windows is restarted?

I was looking at some of the comments View process environment variables in Windows and they make this interesting point.

They make the point that when you set an environment variable from that/the "Environment variables" window, setting system environment variables there. Then it sets them for the explorer.exe process. Hence, any new cmd window receives them. Because the child cmd.exe process spawned from the parent process explorer.exe inherits those variables. And the reason why when you use the SET command in cmd.exe , it is only for that cmd window, is because it only sets them for that particular cmd.exe process. So, comments there make the point that environment variables are always particular to a process.

When Windows is restarted, the environment variables set in that "Environment variables" window persist, such that when Windows is restarted, they are still there. And I know environment variables set there are written into the registry.

I'm wondering, if it is possible to set environment variables for explorer.exe without them going into the registry and so without them being there after Windows restarts?

Note- I ask in my question "without them going into the registry". So a solution that writes them to the registry then deletes them, is not a solution. SETX is not a solution as SETX is just a command line version of what setting variables in the environment variables window does, and it sets them in the registry. The REG command can also be used to add/remove from the registry https://stackoverflow.com/questions/1472722/how-to-remove-an-environment-variable-from-the-system-configuration-with-a-batch or https://stackoverflow.com/questions/13222724/command-line-to-remove-an-environment-variable-from-the-os-level-configuration But it's not a solution / not what i'm speaking of.

barlop
  • 25,198

3 Answers3

1

The "Environment variables" window doesn't really set environment variables for explorer.exe directly – it only broadcasts a "system settings changed" announcement, upon receiving which explorer.exe reloads its environment from the Registry. So there is no standard mechanism for changing its environment without relying on the Registry.

However, you can exit explorer.exe and restart it from your cmd.exe window, which will cause the new Explorer process to inherit all environment variables from its parent again. (Ctrl-Shift-click the taskbar clock and choose "Exit Explorer", then just run explorer from cmd.)

The "System Informer" task manager app (previously called "Process Hacker") can be used to edit environment variables for any process using debug APIs, from within a process' "Properties" window. (Ignore that it groups variables by origin; they still end up in a single per-process list.)

grawity
  • 501,077
1

I can't see any good way to achieve "temporary" explorer.exe variables that will stay if a different tool running SETX, but won't stay on logout/reboot.

i.e. you could use SETX, manually update registry then broadcast "system settings changed", use a debug API, etc, to set the environment variable in explorer.exe process. Then you could manually edit the registry to not contain the new environment variable, so the next time on system startup explorer.exe won't load the environment variable. But I'm pretty confident if another tool causes the "system settings changed" broadcast then EXPLORER will forget about the env var you wanted?


You could create a system startup tool (i.e. with shell:startup or task scheduler or a service) that removes the environment variable with SETX the next time you reboot. But then there would be a race condition because some other system startup processes had already started before your tool. That tradeoff will be fine if you only care about processes started interactively by the user.

1

From what I can tell, it is not possible.

One can see from C# that a process can set its own environment variables Environment.SetEnvironmentVariable("MY_ENV_VARIABLE", "someValue", EnvironmentVariableTarget.Process) https://learn.microsoft.com/en-us/dotnet/api/system.environmentvariabletarget?view=net-7.0

But it can't set another processes. (unless one was able to somehow write to the memory block associated with that process!). (or unless a process reads its environment variables from a file or the registry and other processes change that file or that registry location)

An expert pointed out to me that explorer.exe sets its environment variables based on two lists. That makes sense.

So explorer.exe sees what is in the registry for so-called "user environment variables" and so-called "machine environment variables" (each of which are merely a higher level notion), and sets its environment variables based on those lists.

All or most processes, are spawned from explorer.exe so have explorer.exe as their parent process https://stackoverflow.com/questions/51176823/why-most-processes-have-the-explorer-exe-as-their-parent-process and thus inherit the environment variables from explorer.exe

If you see the answer from Jamie here https://stackoverflow.com/questions/13222724/command-line-to-remove-an-environment-variable-from-the-os-level-configuration Explorer.exe has a way for it to be told to re-read the environment variables from the registry. A powershell script can broadcast a "Windows message (WM_SETTINGCHANGE).". So explorer.exe can update its environment variables without explorer.exe having to be restarted.

So to the question in the title, "Is there a way to set environment variables for explorer.exe that aren't there when windows is restarted?"

The stupid answer is to open the environment variable window, set the environment variable, then explorer.exe will have that environment variable. Then when you no longer want it, open the environment variable window and delete it from there. Then it won't be there when windows restarts.

Of course, I mention in my question

"I'm wondering, if it is possible to set environment variables for explorer.exe without them going into the registry "

Some suggest setx.. But that's just doing from the command line what the environment variable window for adding a variable, so includes writing to the registry. And setx doesn't currently offer an option to delete from the registry one would use eg the reg command for that. And setx would I suppose send the message to let explorer.exe know to re-read from the registry, as the environment variables window probably does. So new processes inherit/see the new environment variables. But like using the above mentioned methods. Reg commands to add/delete from the registry, and a command to broadcast.. It's all using/involving the registry. Because that's the route explorer.exe gives for processes to set its environment variables.

So

The answer has to be No. (aside from somebody somehow hacking the explorer.exe process!)

barlop
  • 25,198