Managing Environment Variables with the Set Command
The term environment variable is foreign to many users because environment variables work in the background to make life easier for both GUI users and those who work at the command line. Admittedly, someone working at the command line is more likely to see the effects of environment variables directly. For example, without a proper path environment variable, you can't execute many applications. The path points to the locations where the command processor should look for applications.
Windows supports two forms of environment variables: permanent and session. The session environment variables only affect the current command line session. As soon as you close the command window, these environment variables are gone. You create session environment variables using the Set command. Permanent environment variables exist for all Windows GUI applications as well as the command prompt. Unless you change them, permanent environment variables exist between reboots of your system as well. You set permanent environment variables using the WMIC Environment alias. For example, if you want to set the permanent path variable to include the C:\Temp directory, you would type WMIC Environment Where Name="Path" SET VariableValue="C:\Temp;%PATH%" and press Enter. Notice that you include the %PATH% expansion variable to preserve the existing path information.
NOTE: Many administrators know that they can set environmental variables using the Environment Variables dialog box. To access this dialog box in a GUI version of Windows, right-click My Computer and choose Properties from the context menu. Select the Advanced tab of the System Properties dialog box. Click Environment Variables to display the Environment Variables dialog box. Unfortunately, Server Core lacks the interface elements required to use this option.
When you work at the command line, you often need environment variables in addition to those Windows uses. For example, the COPYCMD environment variable affects the Copy, Move, and XCopy utilities. You can set the command line switches you want to use with these utilities by setting the COPYCMD environment variable. Of course, you can always override your selections by specifying a different set of command line switches at the command line. For example, if you set the /Y command line switch in the COPYCMD environment variable, you can override it by specifying the /-Y command line switch.
Many environment variables only affect one application. For example, you can set the MORE environment variable to set the command line switches for the More utility. Every time you use the More utility, it searches for this environment variable and sets itself up accordingly.
Command line environment variables need not impose on those used in Windows. Besides setting environment variables in the Environment Variables dialog box and at the command line using the Set command, you can add environment variables to the AutoExec.NT file. Windows executes the commands within AutoExec.NT every time it opens a command prompt for you. These environment variables appear every time you open a command prompt, but won't appear within Windows. You use the Set command within the AutoExec.NT file, just as you would at the command prompt. The "Modifying AutoExec.NT" section of the tutorial describes the AutoExec.NT file in more detail.
For all of the tasks that it performs, the Set command is relatively simple. This command uses the following syntax:
SET [variable[=[string]]] SET /A expression SET /P variable=[promptString]
The following list describes each of the command line arguments.
variable Specifies the environment variable name. The name can't contain spaces or most special characters. You can split words in a variable name using the underscore (_) or dash (-).
string Defines the value of the environment variable. If you type the environment variable, followed by an equals sign (=), but without a value, the Set command deletes the environment variable. Type the Set command followed by the environment variable without an equals sign to determine the current environment variable value. In fact, you can type as little as a single letter to see a list of environment variables and their associated values that begin with that letter.
/A expression Creates an environment variable based on an expression, such as a math equation or the concatenation of multiple environment variables, instead of a standard string.
/P variable=[promptString] Prompts the user to assign a value to an environment variable, rather than assigning the value directly. You must supply a variable name. The optional prompt string lets you provide a specific prompt to the user. Otherwise, the display doesn't show any prompt at all.
Server Core comes with a number of standard environment variables. These hard-coded environment variables perform essential tasks, such as displaying the current directory for you. You can see a list of these environment variables on the Microsoft Web site at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview .mspx. Typing Set by itself and pressing Enter displays the list of environment variables set for your machine. This list doesn't include the hard-coded environment variables such as CD.
You can use the /A command line switch to combine existing environmental variables or even perform math with them. For example, you could use the following Set command to create a new environment variable based on the existing %NUMBER_OF_PROCESSORS% environment variable.
You can use any of the operators shown in Table-4 when creating your expression. The operators appear in order of precedence.
Table-4: Set Expression OperatorsOperator Description () Group expression elements ! ~ - Not, negate, and negative unary operators * / % Multiply, divide, and modulus arithmetic operators + - Add and subtract arithmetic operators << >> Right and left logical shift & Bitwise AND ^ Bitwise exclusive OR | Bitwise OR = *= /= %= += -= &= Assignment operators ^= |= <<= >>= , Expression separator
Any expressions you create can also contain octal, decimal, or hexadecimal numbers. All octal values begin with a 0 (zero) and hexadecimal values begin with a 0x. The Set command is quite handy when you create batch files, so you'll see additional coverage for it in the "Creating Batch Files" section.
In this tutorial:
- CMD and BAT Files
- Starting the Command Interpreter
- Using the CMD Switches
- Working with the Command Interpreter in the Registry
- Modifying Config.NT
- Displaying the Config.NT Commands
- Modifying AutoExec.NT
- Saving Memory Using the LH Command
- Defining Application Compatibility with the SetVer Utility
- Using Common DOS Utilities
- Formatting a Disk with the Format Utility
- Terminating a Session Using the Logoff Utility
- Managing Environment Variables with the Set Command
- Managing Environment Variables with the SetX Utility
- Using Debugging Utilities
- Working at the Command Prompt
- Managing Usernames and Passwords with CmdKey
- Changing Screen Colors with the Color Command
- Obtaining Command Line Help with the Help Utility
- Creating Batch Files
- Using the Choice Command
- Using the For Command
- Using the Goto Command
- Using the Prompt Command
- Using the WaitFor Utility