The set command lets users change one variable or string to another.
Availability
Set is an internal command that is available in the following Microsoft operating systems.
Availability
Set syntax
Set examples
Secret commands
All Versions of MS-DOS
Windows 95
Windows 98
Windows ME
Windows NT
Windows 2000
Windows XP
Windows 7
Windows 8
Windows 10
Windows 11
Set syntax
Windows 2000, XP, and later syntax
Displays, sets, or removes cmd.exe environment variables.
- Windows 2000, XP, and later syntax.
- Windows 2000 and Windows XP Recovery Console syntax.
- MS-DOS, Windows 95, Windows 98, Windows ME syntax.
SET [variable=[string]]
Type SET without parameters to display the current environment variables.
If Command Extensions are enabled SET changes as follows:
SET command invoked with a variable name, no equal sign or value displays the value of all variables whose prefix matches the name given to the SET command. For example:
SET P
would display all variables that begin with the letter ‘P.’
SET command sets the ERRORLEVEL to 1 if the variable name is not found in the current environment.
SET command doesn’t allow an equal sign to be part of the name of a variable.
Two new switches are added to the SET command:
SET /A expressionSET /P variable=[promptString]
The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated. The expression evaluator is pretty simple and supports the following operations, in decreasing order of precedence:
If you use any of the logical or modulus operators, you need to enclose the expression string in quotes. Any non-numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them. If an environment variable name is specified but is not defined in the current environment, then a value of zero is used. Doing this lets you do arithmetic with environment variable values without having to type all those % signs to get their values. If SET /A is executed from the command line outside of a command script, then it displays the final value of the expression. The assignment operator requires an environment variable name to the left of the assignment operator. Numeric values are decimal numbers unless prefixed by 0x for hexadecimal numbers, and 0 for octal numbers. So 0x12 is the same as 18 is the same as 022. Note: the octal notation can be confusing: 08 and 09 are not valid numbers because 8 and 9 are not valid octal digits.
The /P switch lets you set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.
Environment variable substitution is enhanced as follows:
%PATH:str1=str2%
would expand the PATH environment variable, substituting each occurrence of str1 in the expanded result with str2. str2 can be the empty string to effectively delete all occurrences of str1 from the expanded output. str1 can begin with an asterisk, which matches everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1.
May also specify substrings for an expansion.
%PATH:~10,5%
would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified.
%PATH:~-10%
would extract the last 10 characters of the PATH variable.
%PATH:~0,-2%
would extract all but the last 2 characters of the PATH variable.
Finally, support for delayed environment variable expansion was added. This support is always disabled by default, but may be enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?
Delayed environment variable expansion is useful to get around the limitations of current expansion that happens when reading a line of text, not when it is executed. The following example demonstrates the problem with immediate variable expansion:
set VAR=before if “%VAR%” == “before” ( set VAR=after; if “%VAR%” == “after” @echo If you see this, it worked )
would never display the message, because %VAR% is expanded when the lie is read into memory. Changes that occur to the variable after that (such as VAR=after in our compound if statement) is not expanded.
Similarly, the following example will not work as expected:
set LIST= for %i in (*) do set LIST=%LIST% %i echo %LIST%
in that it will NOT build up a list of files in the current directory, but instead set the LIST variable to the last file found. Again, this is because the %LIST% expandeds once the FOR statement is read, and the LIST variable is empty. So the actual FOR loop we are executing is:
for %i in (*) do set LIST= %i
which keeps setting LIST to the last file found.
Delayed environment variable expansion allows a different character (the exclamation mark) to expand environment variables at execution time. If delayed variable expansion is enabled, the above examples could be written as follows to work as intended:
set VAR=before if “%VAR%” == “before” ( set VAR=after if “!VAR!” == “after” @echo If you see this, it worked ) set LIST= for %i in (*) do set LIST=!LIST! %i echo %LIST%
If Command Extensions are enabled, then several dynamic environment variables can be expanded, which don’t show up.
The list of variables displayed by SET. These variable values are computed dynamically each time the value of the variable is expanded. If the user explicitly defines a variable with one of these names, then that definition overrides the dynamic one described below:
%CD% - expands to the current directory string.
%DATE% - expands to the current date using the same format as DATE command.
%TIME% - expands to the current time using the same format as TIME command.
%RANDOM% - expands to a random decimal number between 0 and 32767.
%ERRORLEVEL% - expands to the current ERRORLEVEL value
%CMDEXTVERSION% - expands to the current Command Processor Extensions version number.
%CMDCMDLINE% - expands to the original command line that invoked the command processor.
%HIGHESTNUMANODENUMBER% - expands to the highest NUMA node number on this machine.
Windows 2000 and Windows XP Recovery Console syntax
set [enviroment_variable]=[True/False]
MS-DOS, Windows 95, Windows 98, Windows ME syntax
Displays, sets, or removes Windows environment variables.
Set examples
set path=c:\windows\command
Set the path to c:\windows\command.
set
Display all the set environment variables currently defined.
Users in Microsoft Windows 2000 and Windows XP may have difficulty defining the set values through the MS-DOS prompt. See: How to set the path and environment variables in Windows.
How to use the set command as a substitute for the choice command in Windows 2000 and Windows XP
In the example below, a user would be prompted to enter an option of 1,2, or 3 to print hello, bye, or test.
@ECHO offcls:startECHO.ECHO 1. Print HelloECHO 2. Print ByeECHO 3. Print Testset choice=set /p choice=Type the number to print text.if not ‘%choice%’==’’ set choice=%choice:~0,1%if ‘%choice%’==‘1’ goto helloif ‘%choice%’==‘2’ goto byeif ‘%choice%’==‘3’ goto testECHO “%choice%” is not valid, try againECHO.goto start:helloECHO HELLOgoto end:byeECHO BYEgoto end:testECHO TESTgoto end:end
Secret commands
If you wanted to hide all your directories from users, you could use:
SET DIRCMD=0
The above command prevents anyone from seeing the directories; however, they can still be accessed. To allow the directories to be visible again, type the command below.
SET DIRCMD=
Related information
- How to use choice and set in a batch file.
- See our set definition for further information and related links on this term.