Windows environment variables basics

Perm url with updates: http://xahlee.org/mswin/env_var.html

Windows Environment Variables

Xah Lee, 2009-10-03, 2010-06-18

This document is a basic tutorial about Windows's Environment Variables. The code here is tested on Windows Vista.

Environment variables are system-wide global 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.

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.

Windows env var

The Windows System control panel for editing env vars.

You can launch the app directly in cmd.exe or PowerShell by typing the following (Windows Vista):

c:/Windows/System32/SystemPropertiesAdvanced.exe

Env var names are not case sensitive. So, “PATH”, “Path”, “path” are treated the same.

Sample Values Of Env Var

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
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 (with line break added 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\

Local vs System env vars

A env var can be Local or System. Local means per session, temporary. System means permanent. System env vars are also grouped into User and Machine groups.

Local = Process env var = Per Session = Temporary

Local env var are created by a command shell (cmd.exe or PowerShell).

Local env var are also known as “Process env var”. They are per session env vars. They are temporary.

When you use “set” in cmd.exe to create set a variable, it creates a local env var. When you restart the shell, they are gone.

When programs are launched from the shell, they get all env vars including Process category env vars.

System env vars = User and Machine = Permanent/Registry

System env var are those stored in Windows Registry. They are permanent.

System env var can be created by a command line shell or programing language.

System env vars has 2 categories: User and Machine.

User

The “User” category env vars are things like home dir (HOME), temp dir (TEMP and or TMP).

In Registry, they are at:

HKEY_CURRENT_USER\Environment

Machine

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.

In Registry, they are at:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

For a intro of Registry see: Microsoft Windows Registry Tutorial.

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.

Using cmd.exe to View or Set Env Var

Viewing Env Var

To show a value of a env var, type “echo %‹env var name›%”. For example, to show the “path” env var, do:

echo %path%

To see all your env vars, type:

set

To see all env vars starting with “p”, type:

set p

Setting Env Var For Current Session

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%"

Setting Env Var Permanently

To set env var permanently, use “setx” command. The “setx” command is part of cmd.exe in Windows Vista. Here's a example:

REM example of setting HOME env var
setx HOME "C:\Users\mary"
REM example of adding a path
setx PATH "C:\Program Files (x86)\ErgoEmacs;%PATH%"

For detail, type “setx /?”.

Note: when you set a env var using “setx”, it is set in the registry, however, they are not known in the current session. Restart the shell if you want them to be available.

Managing Env Var With Powershell

Using PowerShell to Manage Environment Variables.

technet.microsoft.com Command shell overview

msdn.microsoft.com Environment Methods

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs