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 )

Standard (built-in) Environment Variables

VariableVolatile

(Read-Only)

Default value in Windows 7/10/2008 (assuming the system drive is C: )
ALLUSERSPROFILE C:ProgramData
APPDATA C:Users

{username}AppDataRoaming

CD

YThe current directory (string).

ClientName

YTerminal servers only – the ComputerName of a remote host.

CMDEXTVERSION

YThe current Command Processor Extensions version number. (NT = “1″, Win2000+ = “2″.)

CMDCMDLINE

YThe original command line that invoked the

Command Processor.

CommonProgramFiles C:Program FilesCommon Files
COMMONPROGRAMFILES(x86) C:Program Files (x86)Common Files
COMPUTERNAME 

{computername}

COMSPEC C:WindowsSystem32cmd.exe or if running a 32 bit

WOW – C:WindowsSysWOW64cmd.exe

DATE

YThe current date using same region specific format as

DATE.

ERRORLEVEL

YThe current ERRORLEVEL value, automatically set when a program exits.
FPS_BROWSER_APP_PROFILE_STRING

FPS_BROWSER_USER_PROFILE_STRING

 Internet Explorer

Default

These are undocumented variables for the Edge browser in Windows 10.

HighestNumaNodeNumberY (hidden)The highest

NUMA node number on this computer.

HOMEDRIVEYC:
HOMEPATHYUsers

{username}

LOCALAPPDATA C:Users

{username}AppDataLocal

LOGONSERVER \{domain_logon_server}

NUMBER_OF_PROCESSORS

YThe Number of processors running on the machine.

OS

YOperating system on the user’s workstation.

PATH

User and

System

C:WindowsSystem32;C:Windows;C:WindowsSystem32Wbem;

{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

YProcessor ID of the user’s workstation.

PROCESSOR_LEVEL

YProcessor level of the user’s workstation.

PROCESSOR_REVISION

YProcessor 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%system32WindowsPowerShellv1.0Modules
Public C:UsersPublic

RANDOM

YA 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 #S

essionNumber. For a console session, SessionName returns “Console".

SYSTEMDRIVE C:
SYSTEMROOT 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 read-only system variable that will resolve to the correct location.

NT 4.0, Windows 2000 and Windows NT 3.1 default to

C:WINNT

TEMP and

TMP

User VariableC:Users

{Username}AppDataLocalTemp

Under XP this was

{username}

Local SettingsTemp

TIME

YThe 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}
USERDOMAIN_roamingprofile The user domain for RDS or standard roaming profile paths. Windows 8/10/2012 (or Windows 7/2008 with

Q2664408)

USERNAME 

{username}

USERPROFILE %SystemDrive%Users

{username}

This is equivalent to the

$HOME environment variable in Unix/Linux

WINDIR 

%WinDir% pre-dates Windows NT and seems to be superseded by %SystemRoot%

Set by default as windir=%SystemRoot%

%windir% is a regular variable and can be changed, which makes it less robust than %systemroot%

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_USEREnvironment

System Variables:

HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment

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)

%__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.

%=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.

%FIRMWARE_TYPE% The boot type of the system:

Legacy ,UEFI,Not implemented ,Unknown Windows 8/2012.

%KEYS%   Related to the (deprecated) KEYS command.

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.