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.

7
  • Maybe you can elaborate on what problem you are trying to solve. I can't think of a reliable way to accomplish this.
    – Carl Walsh
    Commented Jan 20, 2023 at 17:20
  • @CarlWalsh purely curiosity. i think being able to do it does help be closer to the inner workings.
    – barlop
    Commented Jan 20, 2023 at 17:23
  • @CarlWalsh Maybe it can be done in a programming context, via an API? There are cases where a standard tool doesn't offer something but it's easily available from a programming perspective. I did once write my own version of the CHCP command, to view both input and output encodings, or select to change only the input encoding or only the output encoding. Easily doable in C#. But not possible with the (native) CHCP command.
    – barlop
    Commented Jan 20, 2023 at 18:29
  • not sure, but this may help you understand the difference between SET and SETX and how they function: superuser.com/questions/916649/… Commented Jan 20, 2023 at 18:32
  • 1
    Indeed, and thank you for your comment. my point was about the fact that set only affects the current shell and its dependents, but more than that, though I saw you active on this question, i hadn't noticed that you were the Op. my appologies. Commented Jan 20, 2023 at 19:16

3 Answers 3

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.)

5
  • You write "the new Explorer process to inherit all environment variables from its parent again" <-- What are you saying is the parent process of explorer.exe?
    – barlop
    Commented Jan 20, 2023 at 17:15
  • That would be cmd.exe, the same one which you used to re-launch explorer.exe. Any process you run from cmd has cmd as its parent. Commented Jan 20, 2023 at 17:18
  • You write 'it only broadcasts a "system settings changed"'<--so you seem to differ with the idea of a child process inheriting variables from the parent process? A comment on that "view process environment variables"question says "After updating i.e. the registry for the default system PATH env var, windows sends out the WM_SETTINGCHANGE message to all listening windows..Most processes don't listen to that, but EXPLORER has the feature to update it's own env vars, and then any new process EXPLORER creates will have the new env vars.".So that comment suggests there's two ways to get the vars.
    – barlop
    Commented Jan 20, 2023 at 17:22
  • Child processes inherit variables from the parent process when they are started, but the inherited variables are not updated continuously. Each new process gets an independent copy. So any changes made to the environment of either Explorer or Cmd will not affect processes that have been already started from it – only those that will be started after the change. Commented Jan 20, 2023 at 17:25
  • Well yes that's the nature of inheritance!! It occurs on an occasion(like in real life, on birth or on death, or here on birth), not continuous updates. I wasn't suggesting that inheritance meant continuous updates!!!
    – barlop
    Commented Jan 20, 2023 at 17:41
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.

5
  • The question said without adding to the registry so SETX doesn't count.
    – barlop
    Commented Jan 22, 2023 at 16:05
  • You write "if another tool causes the "system settings changed" broadcast then EXPLORER will forget about the env var you wanted?" Well this link stackoverflow.com/questions/13222724/… might have such a broadcast solution. Also re the race condition you mention if a variable is removed from the registry at startup, it could be removed on log off.../ prior to a shutdown/restart. But anyhow, your "setx idea" is writing to the registry so isn't what i'm speaking about.
    – barlop
    Commented Jan 22, 2023 at 17:04
  • +1 for giving me some good insights especially with the comments on the " View process environment variables in Windows" thread that inspired this questionh
    – barlop
    Commented Jan 22, 2023 at 23:36
  • @barlop I appreciate how deep into the details you are getting. Mostly agree with you, but just wanted to point out that you can reliably schedule a task for system startup, but for system shutdown, you can't do anything if somebody pulls the power plug. (AFAIK, maybe there is a Windows API to restore the registry if the system didn't shut down cleanly?) I know changing the registry is out of scope for this question, but I'm confident that it's the only way to accomplish something like this.
    – Carl Walsh
    Commented Jan 27, 2023 at 14:31
  • I don't know any more on this than you do! User1686 suggests a very interesting thing to try in his last paragraph, i've not tried it yet though
    – barlop
    Commented Jan 27, 2023 at 15:59
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!)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .