- How-to: Windows Environment Variables
- Array variables
- Standard (built-in) Environment Variables
- Shell variables
- Undocumented Dynamic variables (read only)
- Undocumented Dynamic variables (read/write)
- Pass variables between batch scripts
- Set PATH and other environment variables in Windows 10
- Set environment variables from command prompt
- Set environment variable by Registry edit
- How to set the path and environment variables in Windows
- Setting the path and variables in Windows 10
- Setting the path and variables in Windows 8
- Setting the path and variables in Windows Vista and Windows 7
- Setting the path and variables in Windows 2000 and Windows XP
- What is the default Windows %PATH%?
- Setting path in the MS-DOS and Windows command line
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) Environment Variables
Variable | Volatile (Read-Only) | Default value assuming the system drive is C: |
---|---|---|
ALLUSERSPROFILE | C:\ProgramData | |
APPDATA | C:\Users\ \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 | ||
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. |
FPS_BROWSER_APP_PROFILE_STRING FPS_BROWSER_USER_PROFILE_STRING |
Default
These are undocumented variables for the Edge browser in Windows 10.
System
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.
C:>
systemroot is a read-only system variable that will resolve to the correct location.
Defaults in early Windows versions are C:\WINNT, C:\WINNT35 and C:\WTSRV
Under XP this was \ \Local Settings\Temp
User Variable
This is equivalent to the $HOME environment variable in Unix/Linux
%windir% is a regular User variable and can be changed, which makes it less robust than %SystemRoot%
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.
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.
Shell variables
Variables for current shell session are created with the SET command and are available only to the current CMD shell. These are only stored in memory not the registry. Shell variables are destroyed when the current shell (CMD) exits.
Running the SET command with no options will display all shell variables but also all User and System environment variables.
If the SET command is used to modify the PATH, or if it is removed completely with PATH ; that will not affect any other programs or CMD sessions opened before or after the current one.
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.
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”
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.
Set PATH and other environment variables in Windows 10
In older windows systems you had to navigate to Advanced System Settings in Control Panel to view, edit or add environmental variables.
- Windows XP — Right-click My Computer, and then click Properties → Advanced → Environment variables → Choose New, Edit or Delete.
- Windows 7 — Click on Start → Computer → Properties → Advanced System Settings → Environment variables → Choose New, Edit or Delete.
In Windows 8 and 10, you can navigate to Advanced System Settings in a similar way.
- Windows 8 — Right click on bottom left corner to get Power User Task Menu → Select System → Advanced System Settings → Environment variables → Choose New, Edit or Delete.
- Windows 10 — Right click on Start Menu to get Power User Task Menu → Select System → Advanced System Settings → Environment variables → Choose New, Edit or Delete.
However, in Windows 10 you can directly get to the Environment Variables window using Search the web and Windows box next to the Start menu. Type environment variables in Windows Search box which gives you two options in search results:
- Edit the system environment variables
- Edit environment variables for your account.
Choose either option and you can add, edit or delete environment variables like PATH.
Set environment variables from command prompt
You can set environment variables from Windows Command Prompt using the set or setx command. The set command only sets the environment variable for the current session. The setx command sets it permanently, but not for the current session. If you want to set it for current as well as future sessions, use both setx and set.
For example, you can set the PATH environment variable permanently (current and future sessions) as below:
To view the current path, run:
- By default setx sets the variable in the local environment (Under HKEY_Current_User Registry key). If you want to set the system variable (Under HKEY_LOCAL_MACHINE registry key), use the flag /m. Start the command prompt as administrator on Windows 10, right click on Start menu and select Command Prompt(Admin).
- The maximum value allowed for a enviroment variable is 1024 characters. So if your variable is long and you try to append to it with setx, you may get a truncated result.
Set environment variable by Registry edit
If your PATH variable is too long, then the best method would be to edit the registry.
For user environment variables, change the key HKEY_CURRENT_USER\Environment . For System Environment variables change
Add, Edit or Delete the environment variable you want to change, then reboot to activate the changes.
How to set the path and environment variables in Windows
Setting the path and environment variables will differ depending on the version of Windows you have on your computer. Choose a link below for your version of Windows.
Administrator privileges are usually required to modify the path and environment variables.
Setting the path and variables in Windows 10
- From the desktop, right-click the very bottom-left corner of the screen to get the Power User Task Menu.
- From the Power User Task Menu, click System.
- In the Settings window, scroll down to the Related settings section and click the System info link.
- In the System window, click the Advanced system settings link in the left navigation pane.
- In the System Properties window, click the Advanced tab, then click the Environment Variablesbutton near the bottom of that tab.
- In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below.
You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New and enter the variable name and variable value.
To view and set the path in the Windows command line, use the path command.
Setting the path and variables in Windows 8
- From the desktop, right-click the very bottom-left corner of the screen to get the Power User Task Menu.
- From the Power User Task Menu, click System.
- Click the Advanced System Settings link in the left column.
- In the System Properties window, click the Advanced tab, then click the Environment Variablesbutton near the bottom of that tab.
- In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below.
You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New and enter the variable name and variable value.
To view and set the path in the Windows command line, use the path command.
Setting the path and variables in Windows Vista and Windows 7
- From the desktop, right-click the Computer icon and select Properties. If you don’t have a Computer icon on your desktop, click Start, right-click the Computer option in the Start menu, and select Properties.
- Click the Advanced System Settings link in the left column.
- In the System Properties window, click the Advanced tab, then click the Environment Variablesbutton near the bottom of that tab.
- In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below.
You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New and enter the Variable name and Variable value.
To view and set the path in the Windows command line, use the path command.
Setting the path and variables in Windows 2000 and Windows XP
The path is now managed by Windows 2000 and Windows XP and not the autoexec.bat or autoexec.nt files, as was done with earlier versions of Windows. To change the system environment variables, follow the steps below.
- From the desktop, right-click My Computer and click Properties. If you don’t have a My Computer icon on your desktop, click Start, right-click the My Computer option in the Start menu, and select Properties.
- In the System Propertieswindow, click the Advancedtab.
- In the Advanced section, click the Environment Variablesbutton.
- In the Environment Variables window (as shown below), highlight the Path variable in the System Variable section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each different directory is separated with a semicolon, as shown below.
You can edit other environment variables by highlighting the variable in the System variables section and clicking Edit. If you need to create a new environment variable, click New and enter the Variable name and Variable value.
To view and set the path in the Windows command line, use the path command.
What is the default Windows %PATH%?
The path is based on programs installed on the computer, so there is no «default path.» However, the Windows minimum path is often the path below.
Keep in mind that as you install programs, the path is updated with the paths for the newly installed programs. So, if you have erased your path after installing other programs, those programs may be affected.
Setting path in the MS-DOS and Windows command line
To view and set the path in MS-DOS and in the Windows command line, use the path command.