Xah Lee, 2009-10-03
This document is a basic tutorial about Windows's Environment Variables.
Environment variables are system-wide variables. They are somewhat like config parameters, and is used by processes. For example, apps need to know the path of your Windows kernal, path to cmd.exe, path of your home dir, paths to search for shell programs, etc. For detail, see: Environment variable.
To view and set your env var, go to Control Panel, then click the “System” icon, then click the “Advanced system settings” link, then “Advanced” tab, Environment Variables button.
The Windows System control panel for editing env vars.
You can launch the app directly in cmd.exe or PowerShell by typing the followinh (Windows Vista):
c:/Windows/System32/SystemPropertiesAdvanced.exe
Env var names are not case sensitive. So, “PATH”, “Path”, “path” are treated the same.
Here's example of env var values as it exists on my system.
Name Value
---- -----
ALLUSERSPROFILE C:\ProgramData
APPDATA C:\Users\xah\AppData\Roaming
CLASSPATH .;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
CommonProgramFiles C:\Program Files\Common Files
CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
COMPUTERNAME XAH-PC
ComSpec C:\Windows\system32\cmd.exe
DFSTRACINGON FALSE
ERGOEMACS_KEYBOARD_LAYOUT dv
FP_NO_HOST_CHECK NO
HOME c:/Users/xah
HOMEDRIVE C:
HOMEPATH \Users\xah
LOCALAPPDATA C:\Users\xah\AppData\Local
LOGONSERVER \\XAH-PC
MSWorksProductCode {15BC8CD0-A65B-47D0-A2DD-90A824590FA8}
NUMBER_OF_PROCESSORS 4
OnlineServices Online Services
OS Windows_NT
Path C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x8...
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PSC1
PCBRAND Pavilion
Platform HPD
PROCESSOR_ARCHITECTURE AMD64
PROCESSOR_IDENTIFIER AMD64 Family 16 Model 2 Stepping 3, AuthenticAMD
PROCESSOR_LEVEL 16
PROCESSOR_REVISION 0203
ProgramData C:\ProgramData
ProgramFiles C:\Program Files
ProgramFiles(x86) C:\Program Files (x86)
PSMODULEPATH C:\Users\xah\Documents\WindowsPowerShell\Modules;C:\Windows\system32\Windows...
PUBLIC C:\Users\Public
PYTHONDOCS C:\Users\xah\na_xruti\python-2.6.2-docs-html
QTJAVA C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
SESSIONNAME Console
SystemDrive C:
SystemRoot C:\Windows
TEMP C:\Users\xah\AppData\Local\Temp
TMP C:\Users\xah\AppData\Local\Temp
TRACE_FORMAT_SEARCH_PATH \\NTREL202.ntdev.corp.microsoft.com\34FB5F65-FFEB-4B61-BF0E-A6A76C450FAA\Tra...
USERDOMAIN xah-PC
USERNAME xah
USERPROFILE C:\Users\xah
windir C:\Windows
One of the most important env var is the PATH. Here's a sample value of path as set in my system:
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\Java\jdk1.6.0_14\bin;C:\hp\bin\Python;c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Windows\system32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\QuickTime\QTSystem\
Here's the same value but formatted for easy reading:
C:\Windows\system32; C:\Windows; C:\Windows\System32\Wbem; C:\Program Files (x86)\Java\jdk1.6.0_14\bin; C:\hp\bin\Python; c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static; C:\Windows\system32\WindowsPowerShell\v1.0\; c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\; c:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\; C:\Program Files (x86)\QuickTime\QTSystem\
On Windows, env vars are classified into 3 categories: Process, User, Machine. Each env var belongs to one category. (but different category can have env var name that also exist in other categories.)
The Process category env vars are per session env vars, or temp env vars. They are typically set once in your current session. When you restart the shell (PowerShell or cmd.exe), they are gone. When programs are launched from the shell, they get all env vars including Process category env vars.
The User and Machine category env vars are permanent env vars. They are stored in the Windows Registry.
User category env vars are things like home dir (HOME), temp dir (TEMP and or TMP).
The Machine category env vars are any other, usually related to your machine or more general info. For example: OS kernal path (WINDIR), processor info (PROCESSOR_ARCHITECTURE, NUMBER_OF_PROCESSORS, etc), application paths (PATH), executable file name extensions (PATHEXT), OS type (OS), current user name (USERNAME), etc.
Note that you can set any new env vars in any category. Programs have access to all your env vars, but which ones are meaningful to the program is up to the program.
To show a value of a env vars, type “echo %‹env var name›%”. For example, to show the “path” env var, do:
echo %path%
To set env var for the current session, use “set ‹var name›=‹value›”. WARNING: make sure there's no space around the equal sign. Example:
set xx=5 echo %xx% REM prints 5. (REM is a comment syntax, everything after it is ignored.)
REM prepending a path to the “path” env var set PATH="C:\Program Files (x86)\ErgoEmacs";%PATH%
To set env var permanently, use “setx” command. The “setx” command is part of cmd.exe in Windows Vista. For example of use, type “setx /?”.
Using PowerShell to Manage Environment Variables.