Environment Variables in Windows

1.  What are Environment Variables?

Environment variables are global system variables accessible by all the processes/users running under the Operating System (OS), such as Windows, macOS and Linux. Environment variables are useful to store system-wide values, for examples,

  • PATH: the most frequently-used environment variable, which stores a list of directories to search for executable programs.
  • OS: the operating system.
  • COMPUTENAMEUSERNAME: stores the computer and current user name.
  • SystemRoot: the system root directory.
  • (Windows) HOMEDRIVEHOMEPATH: Current user’s home directory.

2.  (Windows) Environment Variables

Environment Variables in Windows are NOT case-sensitive (because the legacy DOS is NOT case-sensitive). They are typically named in uppercase, with words joined with underscore (_), e.g., JAVA_HOME.

2.1  Display Environment Variables and their Values

To list ALL the environment variables and their values, start a CMD and issue the command “set“, as follows,

// Display all the variables (in NAME=VALUE pairs)
set
COMPUTERNAME=xxxxxxx
OS=xxxxxxx
PATH=xxxxxxx
.......

Try issuing a “set” command on your system, and study the environment variables listed. Pay particular attention to the variable called PATH.

To display a particular variable, use command “set varname“, or “echo %varname%“:

// Display a particular variable
set COMPUTERNAME
COMPUTERNAME=xxxxxx

// or, use "echo" command with variable name enclosed within a pair of '%'
echo %COMPUTERNAME%
COMPUTERNAME=xxxxxx

2.2  Set/Unset/Change an Environment Variable for the “Current” CMD Session

To set (or change) a environment variable, use command “set varname=value“. There shall be no spaces before and after the '=' sign. To unset an environment variable, use “set varname=“, i.e., set it to an empty string.

set varname set varname=value set varname= setDisplay the value of the variable Set or change the value of the variable (Note: no space before and after ‘=’) Delete the variable by setting to empty string (Note: nothing after ‘=’) Display ALL the environment variables

For examples,

// Set an environment variable called MY_VAR
set MY_VAR=hello
   
// Display
set MY_VAR
MY_VAR=hello
   
// Unset an environment variable
set MY_VAR=
   
// Display
set MY_VAR
Environment variable MY_VAR not defined

An environment variable set via the “set” command under CMD is a local, available to the current CMD session only. Try setting a variable, re-start CMD and look for the variable.

2.3  Using an Environment Variable

To reference a variable in Windows, use %varname% (with prefix and suffix of '%'). For example, you can use the echo command to print the value of a variable in the form “echo %varname%“.

// Display the PATH environment variable
echo %PATH%
PATH=xxxxxxx
   
// Append a directory in front of the existing PATH
set PATH=c:\myBin;%PATH%
PATH=c:\myBin;[existing entries]

2.4  How to Add or Change an Environment Variable “Permanently”

To add/change an environment variable permanently in Windows (so that it is available to ALL the Windows’ processes/users and stayed across boots):

  1. Launch “Control Panel”
  2. “System”
  3. “Advanced system settings”
  4. Switch to “Advanced” tab
  5. “Environment variables”
  6. Choose “System Variables” (for all users)
  7. To add a new environment variable:
    1. Choose “New”
    2. Enter the variable “Name” and “Value”. Instead of typing the “value” and making typo error, I suggest that you use “Browse Directory…” or “Browse File…” button to retrieve the desired directory or file.
  8. To change an existing environment variable:
    1. Choose “Edit”
    2. Enter the new “Value”. Instead of typing the “value” and making typo error, I suggest that you use “Browse Directory…” or “Browse File…” button to retrieve the desired directory or file.

You need to RE-START CMD for the new setting to take effect!

To verify the new setting, launch CMD:

set VAR_NAME
VAR_NAME=VAR_VALUE

2.5  PATH Environment Variable in Windows

When you launch an executable program (with file extension of “.exe“, “.bat” or “.com“) from the CMD shell, Windows searches for the executable program in the current working directory, followed by all the directories listed in the PATH environment variable. If the program cannot be found in these directories, you will get the following error:

// (Windows 2000/XP/Vista/7/8/10) "cmd.exe"
abc
'abc' is not recognized as an internal or external command,
operable program or batch file.
 
// (Windows 95/98) "command.com"
abc
Bad command or file name

To list the current PATH, issue command:

PATH
PATH=path1;path1;path3;...

2.6  How to Add a Directory to the PATH in Windows

To add a directory to the existing PATH in Windows:

  1. Launch “Control Panel”
  2. “System”
  3. “Advanced system settings”
  4. Switch to “Advanced” tab
  5. “Environment variables”
  6. Under “System Variables” (for all users), select “Path”
  7. “Edit”
  8. (For newer Windows 10) A table pops up showing the directories included in the current PATH setting ⇒ “New” ⇒ “Browse…” to select the desired directory to be added to the PATH (Don’t type as you will make typo error!) ⇒ Click “Move Up” repeatedly to move it to the top ⇒ “OK” (Don’t “Cancel”) ⇒ “OK” ⇒ “OK”.
  9. (For older Windows) If you didn’t see a pop-up table, it is time to change your computer.

You need to RE-START CMD for the new PATH setting to take effect!

To verify the new setting, launch CMD:

PATH
PATH=path1;path2;path3;...

Notes:

  • Windows searches the current directory (.) before searching the PATH entries. (Unixes/macOS does not search the current directory, unless you include it in the PATH explicitly.)
  • Windows uses semicolon (;) as the path separator; while Unixes/macOS uses colon (:).
  • If your directory name contains special characters such as space (strongly not recommended), enclosed it with double quotes.