How-to: Windows Environment Variables

Environment variables are mainly used within batch files, they can be created, modified and deleted for a session using the SET command. To make permanent changes, use SETX

Variables can be displayed using either SET or ECHO.

Variables have a percent sign on both sides: %ThisIsAVariable%
The variable name can include spaces, punctuation and mixed case: %_Another Ex.ample%
(This is unlike Parameter variables which only have one % sign and are always one character long: %A )

A variable name may include any of the following characters:
A-Z, a-z, 0-9, # $ ' ( ) * + , - . ? @ [ ] _ ` { } ~
The first character of the name must not be numeric.

Standard (built-in) Variables

Variable Dynamic (update every time used)
Volatile (Set at Logon)
Shell Variable User Environment Variable
(SETX)
System Environment Variable
(SETX /M)
Default value assuming the system drive is C:
ALLUSERSPROFILE     Y     C:\ProgramData
Predefined machine-wide system variable.
APPDATA   Y Y     C:\Users\{username}\AppData\Roaming
CD Y   Y     The current directory (string).
ClientName   Y Y     Terminal servers only - the ComputerName of a remote host.
CMDEXTVERSION Y   Y     The current Command Processor Extensions version number. (NT = "1", Win2000+ = "2".)
CMDCMDLINE Y   Y     The original command line that invoked the Command Processor.
CommonProgramFiles     Y     C:\Program Files\Common Files
COMMONPROGRAMFILES(x86)     Y     C:\Program Files (x86)\Common Files
COMPUTERNAME     Y     {computername}
COMSPEC         Y C:\Windows\System32\cmd.exe or if running a 32 bit WOW - C:\Windows\SysWOW64\cmd.exe
Comspec is used whenever the command shell spawns a new process, e.g. the FOR command, if comspec is not defined those processes will fail to launch.
DATE Y   Y     The current date using same region specific format as DATE.
ERRORLEVEL Y   Y     The current ERRORLEVEL value, automatically set when a program exits.
FPS_BROWSER_APP_PROFILE_STRING
FPS_BROWSER_USER_PROFILE_STRING

    Y     Internet Explorer
Default
These are undocumented variables for the Edge browser in Windows 10.
HighestNumaNodeNumber Y (hidden)   Y     The highest NUMA node number on this computer.
HOMEDRIVE   Y Y     C:
HOMEPATH   Y Y     \Users\{username}
HOMESHARE   Y       Network home folder.
LOCALAPPDATA   Y Y     C:\Users\{username}\AppData\Local
LOGONSERVER   Y Y     \\{domain_logon_server}
NUMBER_OF_PROCESSORS         Y The Number of processors running on the machine.
OneDrive       Y   OneDrive synchronisation folder.
OS         Y Operating system on the user's workstation.
PATH       Y Y C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem;{plus program paths}
PATHEXT  
    Y .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH; .MSC
Determine the default executable file extensions to search for and use, and in which order, left to right.
The syntax is like the PATH variable - semicolon separators.
PROCESSOR_ARCHITECTURE         Y AMD64/IA64/x86 This doesn't tell you the architecture of the processor but only of the current process, so it returns "x86" for a 32 bit WOW process running on 64 bit Windows. See detecting OS 32/64 bit
PROCESSOR_ARCHITEW6432           =%PROCESSOR_ARCHITECTURE% (but only available to 64 bit processes)
PROCESSOR_IDENTIFIER         Y Processor ID of the user's workstation.
PROCESSOR_LEVEL         Y Processor level of the user's workstation.
PROCESSOR_REVISION         Y Processor version of the user's workstation.
ProgramW6432           =%ProgramFiles%(but only available when running under a 64 bit OS)
ProgramData     Y     C:\ProgramData
ProgramFiles     Y     C:\Program Files or C:\Program Files (x86)
ProgramFiles(x86) 1     Y     C:\Program Files (x86)   (but only available when running under a 64 bit OS)
PROMPT     Y     Code for current command prompt format,usually $P$G
C:>
PSModulePath         Y %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\
Public     Y     C:\Users\Public
RANDOM Y         A random integer number, anything from 0 to 32,767 (inclusive).
%SessionName%           Terminal servers only - for a terminal server session, SessionName is a combination of the connection name, followed by #SessionNumber. For a console session, SessionName returns "Console".
SYSTEMDRIVE     Y     C:
SYSTEMROOT     Y     By default, Windows is installed to C:\Windows but there's no guarantee of that, Windows can be installed to a different folder, or a different drive letter.
systemroot is a predefined machine-wide read-only system variable that will resolve to the correct location.
Defaults in early Windows versions are C:\WINNT, C:\WINNT35 and C:\WTSRV
TEMP and TMP       Y Y C:\Users\{Username}\AppData\Local\Temp
Under XP this was \{username}\Local Settings\Temp
TIME Y         The current time using same format as TIME.
UserDnsDomain   Y
  Y   Set if a user is a logged on to a domain and returns the fully qualified DNS domain that the currently logged on user's account belongs to.
USERDOMAIN   Y Y     {userdomain}
USERDOMAIN_roamingprofile   Y       The user domain for RDS or standard roaming profile paths. Windows 8/10/2012.
USERNAME   Y     Y Defined as "SYSTEM", resolves as {username}
USERPROFILE   Y Y     %SystemDrive%\Users\{username}
This is equivalent to the $HOME environment variable in Unix/Linux
WINDIR         Y

Set by default as windir=%SystemRoot%
%WinDir% pre-dates Windows NT, its use in many places has been replaced by the system variable: %SystemRoot%

1 Only on 64 bit systems, is used to store 32 bit programs.

Environment variables are stored in the registry:

User Environment Variables: HKCU\Environment
System Environment Variables: HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

By default, files stored under Local Settings do not roam with a roaming profile.

Dynamic environment variables are computed each time the variable is expanded, this makes them inherently read-only.
When all variables are listed with SET, these will not appear in the list.

Volatile variables are defined under the registry: HKCU\Volatile Environment
Do not attempt to directly SET a volatile variable, while you could change them, the system will overwrite them with newly derived values. The majority of volatile variables are CMD shell variables, so like all CMD shell variables they are stored in memory only.

Precedence

When a new process is started, the variables will be loaded in the following order:

      1. System Environment Variables
      2. Shell Variables (per user)
      3. User Environment Variables
      4. Shell Variables (other)

After the process has started, additional shell variables can be defined with SET, these will be available only to the current CMD shell session, but they will take precedence over any environment variables with the same name.

For example, if the SET command is used to modify the PATH, or if it is removed completely with PATH ; that will affect the current process, but not any other programs or CMD sessions opened before or after the current one.

This precedence is important to understand because if you try to set a System Environment variable PATH = %APPDATA%;C:\Windows, it will fail because the %APPDATA% Shell variable is not created until after the System environment variables are imported to the session.
However, you can use %APPDATA% to build a User environment variable PATH.

Running the SET command with no options will display all Shell variables plus all User and System Environment variables, in other words every variable available to be read by that session. In PowerShell the same list is available via the env: drive

Undocumented Dynamic variables (read only)

%__APPDIR__%   The directory path to the current application .exe, terminated with a trailing backslash. (Global) - discuss
%__CD__%   The current directory, terminated with a trailing backslash. (Global)
%=C:%   The current directory of the C: drive. ( See Raymond Chen's explanation of this.)
%=D:%   The current directory of the D: drive if drive D: has been accessed in the current CMD session.
%DPATH%   Related to the (deprecated) DPATH command.
%=ExitCode%   The most recent exit code returned by an external command, such as CMD /C EXIT n, converted to hex.
%=ExitCodeAscii%   The most recent exit code returned by an external command, as ASCII. (Values 0-32 do not display because those map to ASCII control codes.)
%FIRMWARE_TYPE% The boot type of the system: Legacy, UEFI, Not implemented, Unknown Windows 8/2012.
%KEYS%   Related to the (deprecated) KEYS command.

It is impossible to use SET to define or alter these variables because SET does not allow '=' in a variable name.
More detail on these undocumented variables can be found in this stackoverflow answer from Dave Benham.

Undocumented Dynamic variables (read/write)

%__COMPAT_LAYER%   Set the ExecutionLevel to either RunAsInvoker (asInvoker), RunAsHighest(highestAvailable) or RunAsAdmin(requireAdministrator) for more see elevation and Q286705 / Application Compatibility Toolkit for other Compatibility Layers (colours,themes etc).

Pass variables between batch scripts

There are several ways to pass values between batch files, or between a batch file and the command line, see the CALL and SETLOCAL pages for full details.

A child process by default inherits a copy of all environment variables from its parent, this makes environment variables unsuitable for storing secret information such as API keys or user passwords, especially in rare occasions like crashes where a crash log will often include the full OS environment at the time of the crash. PowerShell/Get-Credential is a more secure approach.

If Command Extensions are disabled, the following dynamic variables will be not accessible:
%CD% %DATE% %TIME% %RANDOM% %ERRORLEVEL% %CMDEXTVERSION% %CMDCMDLINE% %HIGHESTNUMANODENUMBER%

“Men may be convinced, but they cannot be pleased against their will. But though taste is obstinate, it is very variable, and time often prevails when arguments have failed” ~ Samuel Johnson

Related commands:

How-to: Array Variables in CMD.
How-to: User Shell Folders - Profile, Start Menu - Location of user profile folders.
How-to: Detecting 32 vs 64 bit Windows.
CALL - Evaluate environment variables.
SET - View environment variables, set local variables.
SETX - Set environment variables.
How the environment-building process works - Raymond Chen [MSFT].
PowerShell equivalent: Working with Environment variables.
Q100843 - The four types of environment variable.
Q286705 - Set compatibility variables in a batch file.
Q242557 - Registry Settings for Folder Redirection.
StackOverflow - Storing a Newline in a variable.


 
Copyright © 1999-2021 SS64.com
Some rights reserved