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 )

In a batch file the default behaviour is to read and expand variables one line at a time, for example:

 SET /P _price="Enter the price: "
 ECHO %_price%

The above will work, but the version below will fail because %_price% will remain undefined until processing reaches the next line, this behaviour can be changed using SETLOCAL EnableDelayedExpansion

 SET /P _price="Enter the price: " & ECHO %_price%
 ECHO That didnt work!

Standard (built-in) Environment Variables

Variable Volatile
(Read-Only)
Default value in Windows 7/2008
ALLUSERSPROFILE   C:\ProgramData
APPDATA   C:\Users\{username}\AppData\Roaming
CD Y The current directory (string).
ClientName Y Terminal servers only - the ComputerName of a remote host.
CMDEXTVERSION Y The current Command Processor Extensions version number. (NT = "1", Win2000+ = "2".)
CMDCMDLINE Y The original command line that invoked the Command Processor.
CommonProgramFiles   C:\Program Files\Common Files
COMMONPROGRAMFILES(x86)   C:\Program Files (x86)\Common Files
COMPUTERNAME   {computername}
COMSPEC   C:\Windows\System32\cmd.exe or if running a 32 bit WOW - C:\Windows\SysWOW64\cmd.exe
DATE Y The current date using same region specific format as DATE.
ERRORLEVEL Y The current ERRORLEVEL value, automatically set when a program exits.
HighestNumaNodeNumber Y (hidden) The highest NUMA node number on this computer.
HOMEDRIVE Y C:
HOMEPATH Y \Users\{username}
LOCALAPPDATA   C:\Users\{username}\AppData\Local
LOGONSERVER   \\{domain_logon_server}
NUMBER_OF_PROCESSORS Y The Number of processors running on the machine.
OS Y Operating system on the user's workstation.
PATH User and
System
C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem;{plus program paths}
PATHEXT
.COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH; .MSC
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   =%ProgramFiles% (only available on 64 bit systems)
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   =%PROCESSOR_ARCHITECTURE% (only available on 64 bit systems)
ProgramData   C:\ProgramData
ProgramFiles   C:\Program Files
ProgramFiles(x86) 1   C:\Program Files (x86)
PROMPT   Code for current command prompt format,usually $P$G
C:>
PSModulePath   %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\
Public   C:\Users\Public
RANDOM Y A random decimal number between 0 and 32767.
%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   C:
SYSTEMROOT   C:\Windows
TEMP and TMP User Variable 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
User Variable
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   {userdomain}
USERNAME   {username}
USERPROFILE   %SystemDrive%\Users\{username}
WINDIR   C:\Windows

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

Unless stated otherwise, all the variables above are System variables

Environment variables are stored in the registry:

User Variables: HKEY_CURRENT_USER\Environment
System Variables: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

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

Dynamic environment variables are read-only and are computed each time the variable is expanded. When all variables are listed with SET, these will not appear in the list. Do not attempt to directly SET a dynamic variable. 

Undocumented Dynamic variables (read only)

%__CD__%   The current directory, terminated with a trailing backslash.
%=C:%   The current directory of the C: drive.
%=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 hex value of the last return code set by EXIT /B
%=ExitCodeAscii%   The ASCII value of the last return code set by EXIT /B if greater than 32.
%KEYS%   Related to the (deprecated) KEYS command.

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.

“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:

User Shell Folders - Standard folder locations for XP and Windows 7
Detecting 32 vs 64 bit Windows
CALL - Evaluate environment variables
SET - View environment variables, set local variables
SETX - Set environment variables
Q100843 - The four types of environment variable
Q286705 - Set compatibility variables
Q242557 - Registry Settings for Folder Redirection
StackOverflow - Storing a Newline in a variable
Managing Roaming User Data Deployment Guide - Microsoft.com (Word Doc)


© Copyright SS64.com 1999-2014
Some rights reserved