Windows Environment Variables (Legacy Reference)

Setting and reading variables

Environment variables are mainly used within batch files. Create, modify, or delete them for a session with the SET command. For permanent changes, use SETX. Display variables with either SET or ECHO. Variable names take percent signs on both sides, for example %ThisIsAVariable%. The name can include spaces, punctuation, and mixed case, such as %_Another Ex.ample%. This differs from parameter variables, which carry a single percent sign and are always one character long, such as %A.

Standard system variables

The following variables are built into Windows 7, 10, and Server 2008, assuming the system drive is C:. Volatile variables are read-only and computed at expansion time.

Key variables include ALLUSERSPROFILE (C:\ProgramData), APPDATA (C:\Users{username}\AppData\Roaming), COMPUTERNAME, COMSPEC (C:\Windows\System32\cmd.exe), HOMEDRIVE and HOMEPATH, LOCALAPPDATA (C:\Users{username}\AppData\Local), LOGONSERVER, NUMBER_OF_PROCESSORS, OS, PATH (user and system paths separated by semicolons), PATHEXT (executable extensions separated by semicolons), PROCESSOR_ARCHITECTURE (reflects the current process, not the hardware), PROGRAMFILES and PROGRAMFILES(x86), SYSTEMDRIVE, SYSTEMROOT (the actual Windows directory, more robust than WINDIR), TEMP and TMP (C:\Users{Username}\AppData\Local\Temp), USERDOMAIN, USERNAME, USERPROFILE (%SystemDrive%\Users{username}), and WINDIR (legacy, superseded by SYSTEMROOT).

On 64-bit systems, additional variables appear: COMMONPROGRAMFILES(x86), PROCESSOR_ARCHITEW6432, PROGRAMW6432, and PROGRAMFILES(x86). Terminal server sessions expose CLIENTNAME and SESSIONNAME. Dynamic variables such as CD (current directory), DATE, TIME, ERRORLEVEL, RANDOM, and CMDCMDLINE expand to live values.

Registry storage

User variables are stored in HKEY_CURRENT_USER\Environment. System variables are stored in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment. Files under Local Settings do not roam with a roaming profile by default.

Dynamic variables

Dynamic environment variables are read-only and computed each time the variable expands. They do not appear in a SET listing. Do not attempt to SET a dynamic variable directly.

Undocumented read-only dynamic variables include APPDIR (path to the current application .exe with trailing backslash), CD (current directory with trailing backslash), =C: and =D: (current directory per drive), DPATH (related to the deprecated DPATH command), =ExitCode (hex value of the last EXIT /B return code), =ExitCodeAscii (ASCII value of the last EXIT /B return code if greater than 32), FIRMWARE_TYPE (Legacy, UEFI, Not implemented, or Unknown on Windows 8 and Server 2012), and KEYS (related to the deprecated KEYS command).

Undocumented read-write dynamic variables include __COMPAT_LAYER (sets execution level to RunAsInvoker, RunAsHighest, or RunAsAdmin; see elevation documentation and the Application Compatibility Toolkit for other compatibility layers).

Passing variables between scripts

Several methods exist to pass values between batch files or between a batch file and the command line. See the CALL and SETLOCAL documentation for full details. A child process inherits a copy of all environment variables from its parent by default. This inheritance makes environment variables unsuitable for storing secret information such as API keys or user passwords, especially in crash scenarios where a crash log may include the full OS environment at the time of the crash. PowerShell Get-Credential provides a more secure approach for handling secrets.

Back to the blog index