Things are a little more complicated than Microsoft sometimes make it seem.
A lot of the environment variables are stored in the Registry. This means you can query them using the reg
command
Common to all users
reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Specific to current user session
reg query HKCU\Environment
reg query "HKCU\Volatile Environment"
Set by user
If you permanently set your own environment variable using the setx variable value
command it is stored in the registry but not made immediately available.
C:\>setx test removeme
SUCCESS: Specified value was saved.
C:\>reg query "HKCU\Environment"
HKEY_CURRENT_USER\Environment
Path REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
TEMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
TMP REG_EXPAND_SZ %USERPROFILE%\AppData\Local\Temp
test REG_SZ removeme
If you use the set variable=value
command, the variable is available immediately but is not stored in the registry. You cannot use the reg
command to list them although they are shown by set
. These temporary environment variables do not persist after you end the command-prompt session.
Dynamic Environment Variables
There are constructs that act like environment variables that are not stored in the registry in this way. For example:
echo %TIME%
1Just a curiosity: is it possible to refresh the variable previously added to registry? e.g.
setx test removeme
then somethingset test %test%
so that you can have all the variables listed with a simpleset
? I was thinking that it is possible to implement a command/ batch script to make all them explicit upon request... – Hastur – 2017-02-16T11:02:23.490@Hastur: Yes, that would work to make a new variable both immediately available and persistent. – RedGrittyBrick – 2017-02-16T11:05:03.167
@hastur @RedGrittyBrick - it is possible but is definitely not an oneliner in windows cmd. If you install chocolatey it has
refreshenv.cmd
utility which is 10 lines of code and basically it queries and refreshes registry entries – maoizm – 2017-06-08T11:00:41.780Does the
SET
command show Dynamic Environment Variables? I'm trying to find anything and everything that Microsoft's Build Tools set during a session, just wanna cover my bases. – Ungeheuer – 2017-07-10T17:00:11.667