Search notes:

Windows environment variables

Four types of environment variables

There are four types of Windows environment variables
System Found in the registry under HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
User Found in the registry under HKCU\Environment
Volatile Found in the registry under HKCU\Volatile Environment
Process The value of process variables is stored «within» the process and only for the duration of the process.

Querying environment variables (.NET)

The currently set environment variables are exposed in the static methods GetEnvironmentVariable() and GetEnvironmentVariables in the .NET class System.Environment.
When used in PowerShell, the method returns a System.Collections.Hashtable (see also hash tables in PowerShell):
PS C:\> [System.Environment]::GetEnvironmentVariables()
Name                           Value
----                           -----
SESSIONNAME                    Console
ProgramFiles(x86)              C:\Program Files (x86)
USERDNSDOMAIN                  TQ84.LOCAL
…
The value of a particular environment variable can be queried with GetEnvironmentVariable(varName), again in PowerShell:
PS C:\> [System.Environment]::GetEnvironmentVariable('SystemDrive')
C:

Permanently setting an environment variable

Command line approach with PowerShell

The following example creates the local user variable USER_VAR with the value foo and a system wide (global) variable GLOBAL_VAR with the value bar:
[environment]::setEnvironmentVariable('USER_VAR'  , 'foo', 'user'   )
[environment]::setEnvironmentVariable('GLOBAL_VAR', 'bar', 'machine')
It is also possible to set a variable only for the current process:
[environment]::setEnvironmentVariable('PROC_VAR'  , 'baz', 'process')

GUI based approach with control panel

The advanced tab of the System Properties control panel (Control panel -> System -> Change Settings) has a button Environment Variables that allows to view and modify user and system environment variables.
From the command line (cmd.exe), this tab can be opened like so:
control sysdm.cpl,,3
When opening this control panel with PowerShell, the arguments need to be quoted:
control 'sysdm.cpl,,3'

Common variables

Variable Possible value registry
ALLUSERSPROFILE C:\ProgramData Set by wininit.exe, same value as %ProgramData% (at least on Windows 10)
APPDATA C:\Users\Rene\AppData\Roaming HKCU\Volatile Environment
CLIENTNAME HKCU\Volatile Environment\n
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
CommonProgramW6432 C:\Program Files\Common Files
COMPUTERNAME DESKTOP-HELLO
ComSpec C:\Windows\system32\cmd.exe HKLM\…SessMgr\Env…
DriverData C:\Windows\System32\Drivers\DriverData HKLM\…SessMgr\Env…
FPS_BROWSER_APP_PROFILE_STRING Internet Explorer
FPS_BROWSER_USER_PROFILE_STRING Default
HOMEDRIVE C: The combination of HOMEDRIVE and HOMEPATH points to a location where personal files should be stored. It might, but needs not to, be the same directory as %USERPROFILE%. HKCU\Volatile Environment
HOMEPATH \Users\Rene Rene corresponds to %USERNAME%. Compare with %USERPROFILE% HKCU\Volatile Environment
HOMESHARE \\alpha.foo\some\path If HOMEDRIVE is on a UNC path, HOMESHARE contains the its UNC path HKCU\Volatile Environment
LOCALAPPDATA C:\Users\Rene\AppData\Local HKCU\Volatile Environment
LOGONSERVER \\DESKTOP-HELLO HKCU\Volatile Environment
NUMBER_OF_PROCESSORS 2 HKLM\…SessMgr\Env…
OneDrive C:\Users\Rene\OneDrive See also OneDrive HKCU\Environment
OS Windows_NT HKLM\…SessMgr\Env…
PATH
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC HKLM\…SessMgr\Env…
PROCESSOR_ARCHITECTURE AMD64 HKLM\…SessMgr\Env…
PROCESSOR_IDENTIFIER AMD64 Family 21 Model 16 Stepping 1, AuthenticAMD HKLM\…SessMgr\Env…
PROCESSOR_LEVEL 21 HKLM\…SessMgr\Env…
PROCESSOR_REVISION 1001 HKLM\…SessMgr\Env…
ProgramData C:\ProgramData Set by wininit.exe, same value as %ALLUSERSPROFILE (at least on Windows 10)
ProgramFiles C:\Program Files
ProgramFiles(x86) C:\Program Files (x86)
ProgramW6432 C:\Program Files
PUBLIC C:\Users\Public Set by wininit.exe
SESSIONNAME Console HKCU\Volatile Environment\n
SystemDrive C:
SystemRoot C:\Windows
TEMP C:\Users\Rene\AppData\Local\Temp HKCU\Environment overwrites HKLM\…SessMgr\Env…
TMP C:\Users\Rene\AppData\Local\Temp HKCU\Environment overwrites HKLM\…SessMgr\Env…
USERDOMAIN DESKTOP-HELLO HKCU\Volatile Environment
USERDOMAIN_ROAMINGPROFILE DESKTOP-HELLO HKCU\Volatile Environment
USERNAME Rene HKCU\Volatile Environment overwrites HKLM\…SessMgr\Env….
USERPROFILE C:\Users\Rene Compare with %HOMEDRIVE%%HOMEPATH% HKCU\Volatile Environment
windir C:\Windows
Unclear is where the values are defined for

Less common variables

__COMPAT_LAYER Setting __COMPAT_LAYER to RunAsInvoker prevents elevation and thus also suppresses UAC prompts.

PSModulePath

$env:PSModulePath is used by PowerShell to locate modules and consists of the following default paths
In order to load a module that is not located in one of the directories at which $env:PSModulePath points, the import-module cmdlet must be used.

See also

paths.ps1 is a PowerShell script that prints the individual components of the PATH environment variable to the console.

See also

The cmd.exe environment variable %PROMPT%.
Environment variables to control the behavior of cmd.exe built-in commands
In PowerShell, the value of an environment variable can be accessed via the :env scope modifier.
Accessing environment variables with VBScript
The Chocolatey related environment variables.
The value of Environment Variables under Software Environment in msinfo32.exe.

Index