Sign In:
Your IP:

How to Use Windows XP Environment Variables in Windows XP

    Environment Variables in Windows XP

    Environment variables are specially named aliases or placeholders for certain basic system properties that are present for convenience in programming and in system administration. Although they are mostly of interest to technical people, some can be useful to the average PC user and these are discussed here.

    Environment variables have long been used in computer operating systems and are present in Unix, DOS, and Windows The word "environment" used in the context here refers to various features of the computer system and certain basic system data. Here is one of Microsoft's definitions:

    Environment variables are strings that contain information such as drive, path, or file name. They control the behavior of various programs. For example, the TEMP environment variable specifies the location in which programs place temporary files.

    Values for some of these variables are established at login and these are sometimes called predefined variables. They include such parameters as the path and the name of the current user. A table of some of the more useful variables is given below. I have omitted some of the more technical ones. A more complete list is at this Microsoft reference. The variables are enclosed by percent signs when used in scripts or the command line, as is shown in the table. Although the variables are shown in upper case, they are not case-sensitive. More details are given about some of them in subsequent sections.

    (To conform to much of the literature on environment variables, I will be using the term "directories" instead of "folders" in the discussion.)

    Some more common predefined environment variables
    Variable Typical value (May vary, depending on system)
    %ALLUSERSPROFILE% C:\Documents and Settings\All Users
    %APPDATA% C:\Documents and Settings\{username}\Application Data
    %COMPUTERNAME% {computername}
    %COMSPEC% C:\Windows\System32\cmd.exe
    %HOMEPATH% \Documents and Settings\{username}
    %PATH% C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem
    %PATHEXT% .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF: .WSH
    %PROGRAMFILES% Directory containing program files, usually C:\Program Files
    %PROMPT% Code for current command prompt format. Code is usually $P$G
    %SYSTEMDRIVE% The drive containing the Windows XP root directory, usually C:
    %SYSTEMROOT% The Windows XP root directory, usually C:\Windows
    %TEMP% and %TMP% C:\DOCUME~1\{username}\LOCALS~1\Temp
    %USERNAME% {username}
    %USERPROFILE% C:\Documents and Settings\{username}
    %WINDIR% C:\Windows

    The so-called predefined variables are generally unchanged during a login session but there are also some dynamic variables whose value may change. Some of these are listed in the next table.

    Some dynamic environment variables
    Variable Value
    %DATE% Current date in the format determined by the Date command
    %TIME% Current time in the format determined by the Time command
    %CD% Current directory with its full path
    %ERRORLEVEL% Number defining exit status of a previous command or program
    %RANDOM% Random number between 0 and 32767

    Environment variables are very convenient in scripts where certain standard directories and parameters need to be referenced but where the actual locations or names can vary from computer to computer. By having a placeholder, no prior knowledge is required of such details as the exact location of Windows or who is logged in. This is mostly of use to systems administrators but ordinary PC users can also take advantage of environment variables. Some possibilities are explored in the following sections.

    The PATH Environment Variable

    The path to a file is basically its address on the computer. It tells programs how to find a file. It is the drive plus any directories and sub-directories where the file is located. The %PATH% environment variable specifies the command search path. Typically, this is a group of directories where executable files that are repeatedly used are to be found. Examples of the default values are listed in the first table above; in this case, they are the Windows directory and two of its important system sub-directories. To see what is in the PATH variable on a computer, open a command window and enter "echo %PATH%".

    The PATH variable is not immutable and programs like Norton SystemWorks will stick themselves into PATH when they are installed. The PC user can also modify the contents using methods discussed further on. Adding directories to PATH can be very useful if you use scripts or the command line for system maintenance. For example, it can be convenient to put the directory where you keep backups into the PATH variable. It can also be useful to modify PATH if you put programs in a directory different from the usual one.

    In the command line and in batch files, PATH can be modified by the command

        path = dir1;dir2;dir3

    This command will create a PATH environment variable consisting of the three directories dir1, dir2, and dir3. Note that the directory names are separated by a semicolon in the command statement. (An alternate form omits the "equals" sign.) This command will replace any previous directories that were in the PATH variable. To add a directory "newdir" to the current path use the statement

    path = %PATH%;newdir

    It is essential to remember that any changes that are made in a command window only endure until the window is closed. For permanent changes, use the method discussed in another section. It is also possible to use the command-line executable setx.exe that is contained in the Windows XP Service Pack 2 Support Tools.

    Any file that is in a directory listed in the PATH variable can be found or opened by direct reference to the name of the file without having to specify the drive and directories where it is located. This is very handy in scripting and in other ways. For instance, the name of an executable file that is in a directory listed in the PATH variable can be entered into Start-Run to open certain applications (more on this subject here). An example is the executable file for the classic version of the game Solitaire that is generally in the system folder C:\Windows\System32\. The file is sol.exe and its full address with complete path is C:\Windows\System32\sol.exe. Normally this entire string would have to be used in order to reference the Solitaire game. Because its directory is listed in the PATH variable, however, it is sufficient to reference just the file name sol.exe. In fact, because of another environment variable PATHEXT discussed next, simply entering sol into Start-Run is sufficient to open the solitaire game.

    The PATHEXT Variable

    As is explained on other pages, file extensions tell Windows what it is supposed to do with a file. Certain file extensions indicate that the file is executable; that is, the file opens a program or does something. The %PATHEXT% environment variable contains executable file extensions that do not have to be specified for any file in a directory given in the %PATH% variable. The extension .EXE is the most common. Thus the filename sol.exe need only be entered as sol in a command line. If files have the same name but different extensions, the operating system searches in the following order of precedence: .EXE, .COM, .BAT, and .CMD.

    Adding or Editing Environment Variables

    Existing variables can be edited or deleted and new ones can be added in several ways. For temporary changes, the command " Set" can be used in scripts or in a command window. Changes made with "Set" disappear when the command window is closed, thus rendering this method fairly uninteresting for the average PC user. A more useful application of "Set" is to list the environment variables. Open a command window and enter "set" to see what the variables are on your system. Those who wish can consult this Microsoft article for more details about "Set".

    A more permanent way to manage environment variables is provided in  the System Properties dialog box. Open Control Panel-Performance and Maintenance-System (or right-click on My Computer and choose "Properties"). In the box that opens, click the "Advanced" tab to obtain the dialog box shown below. Next, click the button "Environment Variables".

    The figure below shows the "Environment Variables" dialog box that opens next. It lists two kinds of variable- those that apply only to the current user and those that apply to the whole system. You can simply scroll down the lists to see what is on your system or you can edit the lists. Note that I have created a user variable %BACKUP% that gives the path to my one of my backup directories. That makes it easier for me to write a backup script since all I have to do is enter %BACKUP% whenever I want to refer to this directory. To create a new variable, use the "New" button. There are also buttons for editing and for deleting variables.

    The box for adding a new user variable is shown below. Generally, this is likely to be a directory that you use frequently but can be any string of less than 8192 bytes. The maximum total size for all environment variables, including variable names and the "equals" sign, is 65,536 KB.


    The next figure shows a box for editing a variable; in this case it is the PATH variable. Be sure to remember to separate directory names with a semicolon. If you use programs in a particular directory a great deal, you may wish to add it to the path. The figure shows that the directory "G:\Program Files\Support Tools\" has been added. 


    Using the add-on tool Setx.exe

    It is not part of the standard Windows XP setup but a command-line tool called setx.exe is included in the Windows XP Service Pack 2 Support Tools. This tool extends the set command so that permanent changes in the environment variables can be made. For example, to add a folder C:\New Folder to the path, the command would be
          setx path "%PATH%;C:\New Folder"

    Scripts for Listing Environment Variables

    The "Set" command can be used in a command prompt together with a redirection to a text file to make a list of the current environment variables. The command might be
          set > C:\env_list.txt file
    The file name "C:\env_list.txt" can be replaced by any of your choice.

    Microsoft also has a VBScript that lists environment variables on this page.

    Registry Keys for Environment Variables

    For those who are experienced with editing the Registry, there is another way to make changes in environment variables. User environment variables are stored in the Registry in the key


    System variables are found in the key

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

    Note that any environment variable that is in the form that needs to be expanded (for example, %PATH%) must be stored in the registry as a REG_EXPAND_SZ registry value. Editing the Registry is primarily for scripts used by systems administrators and is not recommended for the average PC user.

    Setting Environment Variables in Autoexec.bat

    The file autoexec.bat is a relic from DOS and older versions of Windows but may still be present in some systems. For the most part, Windows XP will ignore any autoexec.bat file but entries defining environment variables will be picked up. Although environment variables can be set in this way, there are probably better ways.

    Published on: 2006-09-11 Author: