0% found this document useful (0 votes)
17 views5 pages

Windows Environment Variables - Windows CMD

Uploaded by

tapon hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Windows Environment Variables - Windows CMD

Uploaded by

tapon hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SS64 CMD How-to Search

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.

Array variables
Unlike PowerShell, which fully supports arrays, there is no built in support for array variables within the CMD shell.
However with some effort you can replicate this functionality using a series of separate variables, named to represent
the array:

Set elem[1]=First element


Set elem[2]=Second one
Set elem[3]=The third one

To perform array indexing operations with these, use EnableDelayedExpansion and a reference like !elem[%var%]!
this is explained fully in this StackOverflow Q/A.

Standard (built-in) Variables


Dynamic
User System
(update
Volatile Shell Environment Environment
Variable every Default value assuming
(Set at Variable Variable Variable
time
Logon) (SETX) (SETX /M)
used)

C:\ProgramData
ALLUSERSPROFILE Y
Predefined machine-wide system varia
APPDATA Y Y C:\Users\{username}\AppData\Roami

CD Y Y The current directory (string).

ClientName Y Y Terminal servers only - the ComputerN

The current Command Processor Exte


CMDEXTVERSION Y Y
Win2000+ = "2".)

CMDCMDLINE Y Y The original command line that invoke

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
C:\Windows\SysWOW64\cmd.exe
Comspec is used whenever the comm
e.g. the FOR command, if comspec is
to launch.

DATE Y Y The current date using same region sp

ERRORLEVEL Y Y The current ERRORLEVEL value, autom

FPS_BROWSER_APP_PROFILE_STRING Internet Explorer


FPS_BROWSER_USER_PROFILE_STRING Y Default
These are undocumented variables fo

Y
HighestNumaNodeNumber Y The highest NUMA node number on th
(hidden)
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

OneDrive Y OneDrive synchronisation folder.

OS Y Operating system on the user's workst

C:\Windows\System32\;C:\Windows\
PATH Y Y
program paths}

.COM; .EXE; .BAT; .CMD; .VBS; .VBE; .J


Determine the default executable file e
PATHEXT Y
and in which order, left to right.
The syntax is like the PATH variable - s

AMD64/IA64/x86 This doesn't tell y


PROCESSOR_ARCHITECTURE Y but only of the current process, so it re
process running on 64 bit Windows. S

PROCESSOR_ARCHITEW6432 =%PROCESSOR_ARCHITECTURE% (bu

PROCESSOR_IDENTIFIER Y Processor ID of the user's workstation

PROCESSOR_LEVEL Y Processor level of the user's workstati

PROCESSOR_REVISION Y Processor version of the user's workst

ProgramW6432 =%ProgramFiles%(but only available

ProgramData Y C:\ProgramData
ProgramFiles Y C:\Program Files or C:\Program Files (

C:\Program Files (x86) (but only avai


ProgramFiles(x86) 1 Y
OS)

Code for current command prompt for


PROMPT Y
C:>

PSModulePath Y %SystemRoot%\system32\WindowsP
Public Y C:\Users\Public

RANDOM Y A random integer number, anything fro

Terminal servers only - for a terminal s


%SessionName% combination of the connection name,
console session, SessionName return
SYSTEMDRIVE Y C:

By default, Windows is installed to C:\


of that, Windows can be installed to a
letter.
SYSTEMROOT Y systemroot is a predefined machine
will resolve to the correct location.
Defaults in early Windows versions are
C:\WTSRV

C:\Users\{Username}\AppData\Local\
TEMP and TMP Y Y
Under XP this was \{username}\Local

TIME Y The current time using same format a

Set if a user is a logged on to a domai


UserDnsDomain Y Y
domain that the currently logged on us
USERDOMAIN Y Y {userdomain}

The user domain for RDS or standard


USERDOMAIN_roamingprofile Y
8/10/2012.
USERNAME Y Y Defined as "SYSTEM", resolves as {use

%SystemDrive%\Users\{username}
USERPROFILE Y Y
This is equivalent to the $HOME enviro

Set by default as windir=%Syste


WINDIR Y %WinDir% pre-dates Windows N
been replaced by the system va

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 the environment-building process works - Raymond Chen [MSFT].


PowerShell - Working with Environment variables.
User Shell Folders - Profile, Start Menu - Location of user profile folders.
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 in a batch file.
Q242557 - Registry Settings for Folder Redirection.
StackOverflow - Storing a Newline in a variable.

Earn $100 per week online


Become a professional book
reviewer. It is completely free.

Copyright © 1999-2021 SS64.com


Some rights reserved

You might also like