Partager via


Set

Displays, sets, or removes environment variables. Used without parameters, set displays the current environment settings.

Syntax

set [[/a [expression]] [/p [variable**=**]] string]

Parameters

/a   : Sets string to a numerical expression that is evaluated.

/p   : Sets the value of variable to a line of input.

variable   : Specifies the variable you want to set or modify.

string   : Specifies the string you want to associate with the specified variable.

/?   : Displays help at the command prompt.

Remarks

  • Using set at the Recovery Console

    The set command, with different parameters, is available from the Recovery Console.

  • Using special characters

    The characters <, >, |, &, ^ are special command shell characters and must be either preceded by the escape character (^) or enclosed in quotation marks when used in string (that is, *"StringContaining&*Symbol"**. If you use quotation marks to enclose a string containing one of the special characters, the quotation marks are set as part of the environment variable value.

  • Using environment variables

    Use environment variables to control the behavior of some batch files and programs and to control the way Windows XP and the MS-DOS subsystem appears and works. The set command is often used in the Autoexec.nt file to set environment variables.

  • Displaying the current environment settings

    When you type the set command alone, the current environment settings are displayed. These settings usually include the COMSPEC and PATH environment variables that are used to help find programs on disk. Two other environment variables used by Windows XP are PROMPT and DIRCMD.

  • Using parameters

    When you specify values for variable and string, the specified variable value is added to the environment and the string is associated with that variable. If the variable already exists in the environment, the new string value replaces the old string value.

    If you specify only a variable and an equal sign (without a string) for the set command, the string value associated with the variable is cleared (as if the variable is not there at all).

  • Using /a 

    The following table lists the operators supported for /a in descending order of precedence.

    Operator

    Operation performed

    < >

    Grouping

    * / % + -

    Arithmetic

    << >>

    Logical shift

    &

    Bitwise AND

    ^

    Bitwise exclusive OR

    |

    Bitwise OR

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

    Assignment

    ,

    Expression separator

    If you use logical (&& ||) or modulus (%) operators, enclose the expression string in quotation marks. Any non-numeric strings in the expression are considered environment variable names whose values are converted to numbers before being 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 do 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 is the same as 022. The octal notation can be confusing. For example, 08 and 09 are not valid numbers because 8 and 9 are not valid octal digits.

  • Using /p 

    You are not required to include a prompt string.

  • Supporting delayed environment variable expansion

    Delayed environment variable expansion support has been added. This support is disabled by default, but you can enable or disable it by using cmd /v.

  • Working with command extensions

    When command extensions are enabled (that is, the default) and you run set alone, it displays all current environment variables. If you run set with a value, it displays the variables that match that value.

  • Using set in batch files

    When creating batch files, you can use set to create variables and use them in the same way that you would the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for set.

  • Calling a set variable from a batch file

    When you call a variable value from a batch file, enclose the value with percent signs (%). For example, if your batch program creates an environment variable named BAUD, you can use the string associated with BAUD as a replaceable parameter by typing %baud% at the command line.

Examples

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

set testVar=test^^1

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

set testVar=test^&1

Set sets the variable value as everything following the equals sign (=). If you type:

set testVar="test^1"

You get the following result:

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:

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 associated with the INCLUDE environment variable:

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:

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

When command extensions are enabled (that is, the default) and you run set with a value, it displays the variables that match that value. For example, if you type set p at the command prompt, you get the following results:

Path=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 8 Stepping 1, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0801
ProgramFiles=C:\Program Files
PROMPT=$P$G

Formatting legend

Format

Meaning

Italic

Information that the user must supply

Bold

Elements that the user must type exactly as shown

Ellipsis (...)

Parameter that can be repeated several times in a command line

Between brackets ([])

Optional items

Between braces ({}); choices separated by pipe (|). Example: {even|odd}

Set of choices from which the user must choose only one

Courier font

Code or program output

Command-line reference A-Z

Command shell overview

Cmd