Windows Environment Variables Tutorial

, , …,

This document is a basic tutorial about Windows's Environment Variables (env var). The code here is tested on Windows 7.

What's Environment Variables

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.

Not Case Sensitive

Windows environment variable names are Not Case Sensitive.

View Environment Variables

In PowerShell, type gci env:. (see: Using PowerShell to Manage Environment Variables.)

In Command Prompt (cmd.exe), type set to see all.

To view via GUI, goto Control Panel\System and Security\System. Click the 〖Advanced system settings〗 link on the left pane, then 〖Advanced〗 tab, then the 〖Environment Variables…〗 button. c:/Windows/System32/SystemPropertiesAdvanced.exe

Windows env var
The Windows System control panel for editing env vars.

Sample Values of Environment Variable

Here's example of env var values as it exists on my system.

Name                           Value
----                           -----
ALLUSERSPROFILE                C:\ProgramData
APPDATA                        C:\Users\h3\AppData\Roaming
asl.log                        Destination=file
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
CommonProgramW6432             C:\Program Files\Common Files
COMPUTERNAME                   H3-HP
ComSpec                        C:\Windows\system32\cmd.exe
ERGOEMACS_KEYBOARD_LAYOUT      dv
FP_NO_HOST_CHECK               NO
FPPUILang                      en-US
home                           \Users\h3
HOMEDRIVE                      C:
homepath                       \Users\h3
LOCALAPPDATA                   C:\Users\h3\AppData\Local
LOGONSERVER                    \\H3-HP
NUMBER_OF_PROCESSORS           4
OnlineServices                 Online Services
OOBEUILang                     en-US
OS                             Windows_NT
Path                           C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program ...
PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PCBRAND                        Pavilion
PERL_JSON_BACKEND              JSON::XS
PERL_YAML_BACKEND              YAML
Platform                       HPD
PROCESSOR_ARCHITECTURE         AMD64
PROCESSOR_IDENTIFIER           AMD64 Family 16 Model 5 Stepping 3, AuthenticAMD
PROCESSOR_LEVEL                16
PROCESSOR_REVISION             0503
ProgramData                    C:\ProgramData
ProgramFiles                   C:\Program Files
ProgramFiles(x86)              C:\Program Files (x86)
ProgramW6432                   C:\Program Files
PSModulePath                   C:\Users\h3\Documents\WindowsPowerShell\Modules;C:\Windows\system32\Win...
PUBLIC                         C:\Users\Public
QTJAVA                         C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
SESSIONNAME                    Console
SystemDrive                    C:
SystemRoot                     C:\Windows
TEMP                           C:\Users\h3\AppData\Local\Temp
TERM                           dumb
TESSDATA_PREFIX                C:\Program Files (x86)\Tesseract-OCR\
TMP                            C:\Users\h3\AppData\Local\Temp
USERDOMAIN                     h3-HP
USERNAME                       h3
USERPROFILE                    C:\Users\h3
VBOX_INSTALL_PATH              C:\Program Files\Oracle\VirtualBox\
windir                         C:\Windows
windows_tracing_flags          3
windows_tracing_logfile        C:\BVTBin\Tests\installpackage\csilogfile.log

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:\Program Files\Common Files\Microsoft Shared\Windows Live;
C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;
C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
c:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;
c:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;
c:\Program Files (x86)\Common Files\Roxio Shared\12.0\DLLShared\;
C:\Program Files (x86)\Windows Live\Shared;
C:\Program Files (x86)\QuickTime\QTSystem\;
C:\Program Files (x86)\Calibre2\;
C:\strawberry\c\bin;
C:\strawberry\perl\site\bin;
C:\strawberry\perl\bin;
C:\Users\h3\AppData\Roaming\npm;
C:\Program Files (x86)\nodejs\;

Local vs System

A env var can be Local or System.

Local Environment Variable = Process Environment Variable = Temporary

Only programs that are launched from that particular shell will be aware of the local env var you've set.

System Environment Variable = Permanent (in Windows Registry)

System env vars has 2 categories: {User, 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.

〔➤ 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.

View or Set Environment Variable using cmd.exe

Viewing Environment Variable

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 Environment Variable 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 Environment Variable 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 “cmd.exe” if you want them to be available.

Managing Environment Variables using Powershell

Using PowerShell to Manage Environment Variables.

References

blog comments powered by Disqus