Set


Applies To: Windows Server 2008,Windows Server 2008 R2,Windows XP,Windows Server 2003,Windows Vista,Windows Server 2000,Windows Server 2003 R2,Windows 7

Displays, sets, or removes CMD.EXE environment variables. If used without parameters, set displays the current environment variable settings.

For examples of how to use this command, see Examples.

Syntax


Copy Code

set [<Variable>=[<String>]]
set [/p] <Variable>=[<PromptString>]
set /a <Variable>=<Expression>

Parameters

Parameter

Description

<Variable>

Specifies the environment variable to set or modify.

<String>

Specifies the string to associate with the specified environment variable.

/p

Sets the value of Variable to a line of input entered by the user.

<PromptString>

Optional. Specifies a message to prompt the user for input. This parameter is used with the /p command-line option.

/a

Sets String to a numerical expression that is evaluated.

<Expression>

Specifies a numerical expression. See Remarks for valid operators that can be used in Expression.

/?

Displays help at the command prompt.

Remarks

Operator

Operation performed

( )

Grouping

! ~ -

Unary

* / %

Arithmetic

+ -

Arithmetic

<< >>

Logical shift

&

Bitwise AND

^

Bitwise exclusive OR

|

Bitwise OR

= *= /= %= += -= &= ^= |= <<= >>=

Assignment

,

Expression separator

If you use logical (&& or ||) or modulus (%) operators, enclose the expression string in quotation marks. Any non-numeric strings in the expression are considered environment variable names, and their values are converted to numbers before they are processed. If you specify an environment variable name that is not defined in the current environment, a value of zero is allotted, which allows you to perform arithmetic with environment variable values without using the % to retrieve a value.

If you run set /a from the command line outside of a command script, it displays the final value of the expression.

Numeric values are decimal numbers unless prefixed by 0× for hexadecimal numbers or 0 for octal numbers. Therefore, 0×12 is the same as 18, which is the same as 022.

Examples

To set an environment variable named TEST^1, type:


Copy Code

set testVar=test^^1

Note

The set command assigns everything that follows the equal sign (=) to the value of the variable. If you type:


Copy Code

set testVar="test^1"

You get the following result:


Copy Code

testVar="test^1"

To set an environment variable named TEST&1, type:


Copy Code

set testVar=test^&1

To set an environment variable named INCLUDE so that the string C:\Inc (the \Inc directory on drive C) is associated with it, type:


Copy Code

set include=c:\inc

You can then use the string C:\Inc in batch files by enclosing the name INCLUDE with percent signs (%). For example, you might include the following command in a batch file so that you can display the contents of the directory that is associated with the INCLUDE environment variable:


Copy Code

dir %include%

When this command is processed, the string C:\Inc replaces %include%.

You can also use set in a batch program that adds a new directory to the PATH environment variable. For example:


Copy Code

@echo off
rem ADDPATH.BAT adds a new directory
rem to the path environment variable.
set path=%1;%path%
set

To display a list of all of the environment variables that begin with the letter P, type:


Copy Code

set p

Note

This command requires command extensions, which are enabled by default.

Additional references

Command-Line Syntax Key