Shell Scripting 101, Lesson 2

In Lesson 1, I introduced you to the Windows shell scripting language. You can use this scripting language to write scripts (i.e., .bat or .cmd files), such as those you wrote in Lesson 1. You can trace the .bat extension back to the batch commands in MS-DOS. Batch files for earlier OSs such as MS-DOS and Windows 95 had limited functionality. Starting with Windows NT, however, the shell scripting language became much more powerful. With this new functionality came the new .cmd extension.

In Windows 2000 and NT, you can save and run code with a .bat or .cmd extension. Whether a .bat or .cmd file, the code runs the same. However, if you create a .cmd file in a Win2K or NT system, it won’t work on an earlier OS. In addition, if you write and save code as a .bat file on Win2K or NT but run it on an earlier OS, some commands might have limited functionality or might not work. So, if you’re writing scripts for a mixed environment, you need to test those scripts carefully to make sure they function correctly. Scripts obtain information from four basic sources:

  • Information you write, or hard-code, into the script
  • Information from environment variables
  • Information from arguments
  • Information from executed commands (i.e., command output)

In this lesson, I discuss how you can hard-code information into your scripts and how you can use environment variables. I’ll discuss how to use arguments in Lesson 3 and how to use command output in Lesson 4.

Hard-Coding Information
In a script, you often have to specify computer or user information (e.g., drive, filename, username). When you code this information directly into your script, you’re hard-coding the information. Take, for example, the code

Echo My NT installation is in the C:\winnt directory.

In this code, the pathname C:\winnt is hard-coded.

Using Environment Variables
NT’s online Help file defines an environment variable as, "A string consisting of environment information, such as a drive, path, or filename, associated with a symbolic name that can be used by Windows NT." Environment variables let you easily access environment information that the registry stores. The registry stores system-related environment information in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment subkey and user-related environment information in the HKEY_CURRENT_USER\Environment subkey.

You can use the Set command to see the environment variables defined on your computer. Open the command shell window, type


at the command prompt, then press Enter. A list of the environment variables appears.

You can use the Control Panel System applet to add more environment variables. Open the applet, and select the Environment tab. Type the environment variable’s symbolic name in the Variable field and its environment information in the Value field. Click Set.

In a script, you can use an environment variable’s symbolic name to access that variable’s value. For example, if you run the code

Echo My NT installation is in the C:\winnt directory.

in the command shell window, you receive the message My NT installation is in the C:\winnt directory. Instead of hard-coding the installation pathname, you can use the environment variable that contains this information: SYSTEMROOT. To retrieve and use an environment variable's value in code, you need to enclose the environment variable's symbolic name in percent (%) signs. So, to retrieve the NT installation pathname, you can run the code

Echo My NT installation is in the %SYSTEMROOT% directory.

If you installed NT in the default location, you receive the message My NT installation is in the C:\winnt directory. If you’ve changed the location, the message will display that pathname instead of C:\winnt.

Let’s look at another example. Suppose you want to create a text file, test.txt, in the Temp directory on your computer. If you run the code

Echo Here is the test file you created. > C:\temp\test.txt

the system creates test.txt in the C:\temp folder; this file has one line that reads Here is the test file you created. However, if your Temp folder isn’t in the specified location (i.e., C:\temp), this code will fail. To avoid possible failure, you can use the TEMP environment variable instead of hard-coding the Temp folder’s location. If you run the code

Echo Here is the test file you created. > %TEMP%\test.txt

the system creates test.txt in the Temp folder, no matter the location of that folder.

In the last two code examples, note the use of the greater than (>) sign. The > sign is a redirection symbol that tells the system to redirect the output of the preceding command (in this case, the Echo command) to the specified file.

Another useful environment variable is USERPROFILE, which contains profile information about the currently logged-on user. For example, you can use this variable with the Dir command. The Dir command lists all the subdirectories and files in the directory you specify. If you specify USERPROFILE as the directory with the code


you receive the contents of the Profiles directory for the currently logged-on user. If you want the username of the currently logged-on user and the name of the computer the user is logged on to, you can run the code

Echo A user called %USERNAME% is logged into %COMPUTERNAME% now.

As these examples show, environment variables are handy for retrieving information about currently logged-on users and their computers. Environment variables are also handy for temporarily holding a value so that you can pass information to script as an argument. I cover how to use environment variables for this purpose in Lesson 3.

Practice Exercises

  1. Run the Set command from the command prompt to view the environment variables on your computer.
  2. Run the Set command and output the results to a text file. (Here’s a hint: Use the > redirection symbol.)
Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.