In Windows (e.g. version 10) you can list environment variables using the command
set
How do you list just the environment variables that are the same for all users or just the environment variables that are for the current user or session?
In Windows (e.g. version 10) you can list environment variables using the command
set
How do you list just the environment variables that are the same for all users or just the environment variables that are for the current user or session?
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
reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
reg query HKCU\Environment
reg query "HKCU\Volatile Environment"
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.
There are constructs that act like environment variables that are not stored in the registry in this way. For example:
echo %TIME%
For clean output without extra lines and black spaces use the following commands in cmd.exe:
User environment variables:
FOR /F "tokens=1,3* skip=2" %G IN ('reg query HKCU\Environment') DO @echo %G=%H %I
Global (system) environment variables:
FOR /F "tokens=1,3* skip=2" %G IN ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"') DO @echo %G=%H %I
It works in Windows 7-10, and most probably in earlier versions as well.
In the terminal, type the command: echo %temp%
If the "temp" environment variable contains "H:\Users\amacm\AppData\Local", Windows will respond with the following:
H:\Users\amacm\AppData\Local
If there is no environment variable named "temp", Windows will respond with the following:
%temp%