Environment Variables in Windows XP

Page 1 of 8

Computer Education
WinXP Internet General Downloads Home

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.)

Table I. Some more common predefined environment variables Variable % ALLUSERSPROFILE% C:\Documents and Settings\{username}\Application %APPDATA% Data Typical value (May vary, depending on system)

C:\Documents and Settings\All Users

http://vlaurie.com/computers2/Articles/environment.htm

8/11/2009

. usually C:\Windows C:\DOCUME~1\{username}\LOCALS~1\Temp {username} C:\Documents and Settings\{username} 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. Table II. . .C:\Windows\. Code is usually $P$G The drive containing the Windows XP root directory.WSF. usually C:\Program Files Code for current command prompt format. .VBS. Some of these are listed in the next table. Some dynamic environment variables Variable %DATE% %TIME% %CD% %ERRORLEVEL% %RANDOM% Value Current date in the format determined by the Date command Current time in the format determined by the Time command Current directory with its full path Number defining exit status of a previous command or program Random number between 0 and 32767 http://vlaurie.COM. .CMD. usually C: The Windows XP root directory.BAT. .EXE. . .JS .C:\Windows\System32\Wbem %PATHEXT% %PROGRAMFILES% %PROMPT% %SYSTEMDRIVE% %SYSTEMROOT% %TEMP% and %TMP% %USERNAME% %USERPROFILE% %WINDIR% .VBE.WSH Directory containing program files.exe C: \Documents and Settings\{username} C:\Windows\System32 %PATH% \.htm 8/11/2009 .Environment Variables in Windows XP Page 2 of 8 %COMPUTERNAME% %COMSPEC% %HOMEDRIVE% %HOMEPATH% {computername} C:\Windows\System32\cmd.com/computers2/Articles/environment.

open a command window and enter "echo %PATH%".(Note that the presence of spaces in some names may necessitate the use of enclosing quotation marks around environment variables in scripts.newdir It is essential to remember that any changes that are made in a command window only endure until the window is closed. To see what is in the PATH variable on a computer.exe that is contained in the Windows XP Service Pack 2 Support Tools. it can be convenient to put the directory where you keep backups into the PATH variable.Environment Variables in Windows XP Page 3 of 8 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. For instance.htm 8/11/2009 . The PATH Environment Variable The path to a file is basically its address on the computer. Adding directories to PATH can be very useful if you use scripts or the command line for system maintenance.dir3 This command will create a PATH environment variable consisting of the three directories dir1. The PATH variable is not immutable and programs like Norton SystemWorks will stick themselves into PATH when they are installed.) By having a placeholder. use the method discussed in another section. The PC user can also modify the contents using methods discussed further on. 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). Some possibilities are explored in the following sections.com/computers2/Articles/environment. This is very handy in scripting and in other ways. The %PATH% environment variable specifies the command search path. (An alternate form omits the "equals" sign. It tells programs how to find a file. To add a directory "newdir" to the current path use the statement path = %PATH%. It is the drive plus any directories and sub-directories where the file is located. this is a group of directories where executable files that are repeatedly used are to be found. in this case. It can also be useful to modify PATH if you put programs in a directory different from the usual one. PATH can be modified by the command path = dir1. Examples of the default values are listed in the first table above. It is also possible to use the command-line executable setx. they are the Windows directory and two of its important system sub-directories. and dir3. An example is http://vlaurie. In the command line and in batch files. Typically.) This command will replace any previous directories that were in the PATH variable. dir2. 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. Note that the directory names are separated by a semicolon in the command statement. For example.dir2. no prior knowledge is required of such details as the exact location of Windows or who is logged in. For permanent changes.

Normally this entire string would have to be used in order to reference the Solitaire game. the %TEMP% directory has been changed from the default value. that is. http://vlaurie.COM. Those who wish can consult this Microsoft article for more details about "Set". Note that the there must be no spaces on either side of the "equals" sign. however. In fact. The file is sol.BAT. An example is shown below: set temp=C:\temp Here. click the "Advanced" tab to obtain the dialog box shown below. The extension . The PATHEXT Variable As is explained on other pages. .exe need only be entered as sol in a command line. For temporary changes. Certain file extensions indicate that the file is executable. and . the operating system searches in the following order of precedence: . because of another environment variable PATHEXT discussed next. Next.exe. 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").htm 8/11/2009 . Adding or Editing Environment Variables Existing variables can be edited or deleted and new ones can be added in several ways.EXE. A more useful application of "Set" is to list the environment variables. In the box that opens. 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. file extensions tell Windows what it is supposed to do with a file.Environment Variables in Windows XP Page 4 of 8 the executable file for the classic version of the game Solitaire that is generally in the system folder C:\Windows\System32\.EXE is the most common. Because its directory is listed in the PATH variable. it is sufficient to reference just the file name sol. the command " Set" can be used in scripts or in a command window.com/computers2/Articles/environment. If files have the same name but different extensions. simply entering sol into Start-Run is sufficient to open the solitaire game. thus rendering this method fairly uninteresting for the average PC user.exe. Thus the filename sol. the file opens a program or does something. click the button "Environment Variables".CMD. Open a command window and enter "set" to see what the variables are on your system. Changes made with "Set" disappear when the command window is closed.exe and its full address with complete path is C:\Windows\System32 \sol.

use the "New" button. There are also buttons for editing and for deleting variables.htm 8/11/2009 .com/computers2/Articles/environment.Environment Variables in Windows XP Page 5 of 8 The figure below shows the "Environment Variables" dialog box that opens next. 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. http://vlaurie. It lists two kinds of variablethose 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. To create a new variable.

you may wish to add it to the path. If you use programs in a particular directory a great deal.htm 8/11/2009 . The figure shows that the directory "G:\Program Files\Support Tools\" has been added. The maximum total size for all environment variables. is 32767 characters. The next figure shows a box for editing a variable. Be sure to remember to separate directory names with a semicolon. in this case it is the PATH variable. including variable names and the "equals" sign.Environment Variables in Windows XP Page 6 of 8 The box for adding a new user variable is shown below. this is likely to be a directory that you use frequently but can be any string of less than 8192 bytes. http://vlaurie.com/computers2/Articles/environment. Generally.

txt The file name "C:\env_list.txt" can be replaced by any of your choice. to add a folder C:\New Folder to the path. Microsoft also has a VBScript that lists environment variables on this page.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.exe is included in the Windows XP Service Pack 2 Support Tools. The command might be set > C:\env_list. User environment variables are stored in the Registry in the key: HKEY_CURRENT_USER\Environment 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.com/computers2/Articles/environment. the command would be setx path "%PATH%. This tool extends the set command so that permanent changes in the environment variables can be made. Setting Environment Variables in Autoexec. For example.htm 8/11/2009 .Environment Variables in Windows XP Page 7 of 8 Using the add-on tool Setx. Editing the Registry is primarily for scripts used by systems administrators and is not recommended for the average PC user. Registry Keys for Environment Variables For those who are experienced with editing the Registry.bat http://vlaurie.exe It is not part of the standard Windows XP setup but a command-line tool called setx. there is another way to make changes in environment variables.

Environment variables in Vista The same general considerations hold but there are differences in the details of environment variables for Vista.Environment Variables in Windows XP Page 8 of 8 The file autoexec.com/computers2/Articles/environment.htm 8/11/2009 . Home page ©2002-2008 Victor Laurie This page last updated on: 02/21/2008 04:14:33 Main XP page > http://vlaurie.bat file but entries defining environment variables will be picked up. there are probably better ways.bat is a relic from DOS and older versions of Windows but may still be present in some systems.com. Windows XP will ignore any autoexec. Although environment variables can be set in this way. These are discussed at vistaonwindows. For the most part.