1

I know how to find environment variables on my Windows 10 machine.

However, there are other variables that are listed when I run set in cmd.exe and when I run the set ENV_VAR=val command in cmd.exe, it stores additional environment variables that do not seem to be stored in the same location as global and user environment variables.

Example ones are: APPDATA, HTTP_PROXY, SystemRoot, USERDNSDOMAIN

Where are these being stored?

Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
  • Can you provide the list of those extra variables? – RobertBaron Jun 06 '19 at 12:45
  • Some of these variables could have been created by scripts, etc. that ran when you logged in. – RobertBaron Jun 06 '19 at 12:55
  • 2
    The full environment gets loaded by WINAPI functions such as `CreateEnvironmentBlock` or the private shell32 function `RegenerateUserEnvironment`. – Eryk Sun Jun 06 '19 at 13:29
  • 2
    `CreateEnvironmentBlock` pulls from multiple sources: "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion", "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\", "HKU\\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders", "HKU\\Environment", "HKU\\Volatile Environment\", `GetUserNameExW`, and `GetComputerNameExW`. – Eryk Sun Jun 06 '19 at 13:30
  • 1
    I could break the variables down by source in an answer, but most of them shouldn't be changed directly. Where they're currently stored should be seen as an opaque implementation detail. – Eryk Sun Jun 06 '19 at 13:35

3 Answers3

2

The cmd.exe shell creates some pseudo-variables dynamically. These are not statically defined. I do not know about HTTP_PROXY.

CD
RANDOM
DATE
TIME
ERRORLEVEL

The others are system controlled. It is unlikely that any good would come from changing them.

APPDATA
SystemRoot
USERDNSDOMAIN
lit
  • 14,456
  • 10
  • 65
  • 119
1

https://winsourcecode.blogspot.com/2019/05/listenvironmentexe-list-system-user.html is a program that lists System, User, and Volatile variables, and the resulting process environment variables. It also lists the Dynamic variables detailed in help.

--------
System
--------
ComSpec=%SystemRoot%\system32\cmd.exe
DriverData=C:\Windows\System32\Drivers\DriverData
Link=/pdb:none /MAPINFO:LINES
NUMBER_OF_PROCESSORS=4
Etc

--------
Volatile - These are set at logon
--------
LOGONSERVER=\\DESKTOP-UCDGI39
USERDOMAIN=DESKTOP-UCDGI39
HOMEDRIVE=C:
USERDOMAIN_ROAMINGPROFILE=DESKTOP-UCDGI39
Etc    

--------
User - These override system variables, and in the case of PATH are added to the system PATH
--------
MSDevDir=C:\Program Files (x86)\Microsoft Visual Studio\Common\MSDev98
Etc

--------
Process - This is the combined environment from the above for the program
          Variables starting with an equals sign, such as =C:=C:\Windows are internal CMD variables
          CMD simulates a default directory per drive like MSDos. This is how it keeps track
--------
=C:=C:\Windows\System32
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=DESKTOP-UCDGI39
ComSpec=C:\WINDOWS\system32\cmd.exe
DriverData=C:\Windows\System32\Drivers\DriverData
HOMEDRIVE=C:
HOMEPATH=\Users\David Candy
Etc

--------
Dynamic - These are updated each time they are used
--------
CD
DATE
TIME
RANDOM
ERRORLEVEL
CMDEXTVERSION
CMDCMDLINE
HIGHESTNUMANODENUMBER

Environment variables are created at logon and passed to the shell Explorer.exe. From now on they are only in memory. Any program Explorer starts, incl cmd.exe gets a copy of explorer's environment memory. If you start a program from cmd.exe it gets a copy of CMD's environment memory. Programs cannot access other program's memory so it is ONLY one way. If a program changes a variable only that program can see, or programs that it starts after changing it. The program's memory block is thrown away when the program exits.

SetX notifies all programs if the environment is changed. However the ONLY well known program that listens for this is Explorer.exe. CMD does not listen for this message. See https://learn.microsoft.com/en-us/windows/desktop/winmsg/wm-settingchange for how it does this. So after using SetX or your program sending the message, new programs, incl CMD.exe, started by Explorer will have the changes. Existing programs won't. Existing will start other programs with a copy of their now old environment block.

A common question is how to run setx and also have it take effect immediately. The answer is you can't. So you run Setx for the future and Set for the current CMD.exe instance.

Noodles
  • 264
  • 2
  • 3
  • I note that the TEMP variable is in three (3) sets on my machine; System, User, and Process. – lit Jun 07 '19 at 18:28
  • System is the base, and is overridden by user, the resultant variables for your program are the process. Process is what you use. System and User are where they came from. – Noodles Jun 07 '19 at 18:32
0

Be careful: when you launch SET command in your command prompt, then they just stay within your prompt, but once you close that prompt, those are lost, so in fact, they are just stored in the RAM of your command prompt.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • [`setx`](https://ss64.com/nt/setx.html) sets variables permanently via the registry (but does not affect the current `cmd.exe` instance)... – aschipfl Jun 06 '19 at 13:20
  • @aschipfl: my point exactly : `setx` sets it persistently, while `set` is just used for the session itself. – Dominique Jun 06 '19 at 13:24