Environment Variables

Tools that command shell script writers can't live without

Michael Otey

February 24, 2003

3 Min Read
ITPro Today logo in a gray background | ITPro Today

Environment variables are some of the most useful tools available to command shell script writers. By default, all Windows systems include a host of environment variables that can provide important information about the system and the current user. To use environment variables in scripts, you enclose the environment variable name within percent signs (e.g., %username%). To list all your system's environment variables, you can type

set

at the command prompt. You can also use the Set command to create new environment variables. For example,

set mynewvariable = newvalue

creates an environment variable named mynewvariable and assigns it the value newvalue. When you use the Set command to create variables, those variables are available only in the current command window. To create global environment variables, open the Control Panel System applet, click Advanced, open the Environment Variables dialog box, then use the dialog box to create the new environment variables. This month, I list the 10 environment variables most widely used in command shell scripts.

10. ERRORLEVEL—One of the most important environment variables for script writers, the ERRORLEVEL variable contains the numerical value of the most recent program exit code. An error code of 0 is almost universally regarded as successful. Other ERRORLEVEL values vary according to the application. Some programs don't return ERRORLEVEL codes, so if you use this environment variable in a script, be sure you test each program the script calls to determine whether the program supports the ERRORLEVEL variable.

9. USERDOMAIN—The USERDOMAIN environment variable contains the name of the Windows domain that the user is currently logged on to. You'll likely find this variable useful for displaying information to the user as well as for connecting to network resources.

8. HOMEDRIVE and HOMEPATH—Two closely related environment variables, HOMEDRIVE and HOMEPATH contain the drive letter and the path, respectively, that are defined in the user's profile. The HOMEPATH variable doesn't include the drive letter.

7. LOGONSERVER—The LOGONSERVER variable contains the name of the Windows system that processed the current user's logon. That system is typically a domain controller (DC); for Workgroup-based systems, it can alternatively be the current user's system.

6. TEMP—TEMP contains the path to the directory in which system processes build temporary work files. You can also use this path for temporary files that your script needs to build.

5. PATH—The PATH variable is one of the most important environment variables for system function. The system uses the PATH variable when it attempts to locate a program or script to execute. The PATH variable typically contains multiple semicolon-separated paths that point to the directories in which the system will search for the executable.

4. SYSTEMROOT—The SYSTEMROOT environment variable contains the path to your Windows system directory. This variable can help you determine where Windows system files reside, run Windows applets, and locate system DLLs.

3. OS—You can use the OS environment variable to determine the level of the OS that your script is running on. On Windows XP, Windows 2000, and Windows NT systems, this variable will contain the value of WINDOWS_NT. On Windows 9x systems, the OS variable isn't present by default. This variable comes in handy when your script needs to run on multiple OSs and needs to process commands that might be valid on XP, Win2K, and NT but not on Win9x.

2. COMPUTERNAME—As its name implies, the COMPUTERNAME variable contains the name of the Windows system as it's specified in the Control Panel Network applet. Scripts that need to process lists of remote systems or perform unique actions on different computers often access the COMPUTERNAME environment variable.

1. USERNAME—Undoubtedly the most commonly used environment variable, the USERNAME variable contains the name of the user who's currently logged on to the system. You can take advantage of the USERNAME variable to create custom-named files and directories and to display personalized runtime information to the end user.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like