Posted On 2007-04-24 by FortyPoundHead
Keywords: Command Reference
Tags: Windows Commandline Windows
Views: 1804

Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.

SET variable
SET variable=string
SET /A variable=expression
SET variable=
SET /P variable=[promptString]

variable : A new or existing environment variable name
string : A text string to assign to the variable.
expression: : Arithmetic Sum

Also see SetX, VarSearch and VarSubstring for more advanced variable manipulation.Variable names are not case sensitive but the contents can be. Variables can contain spaces.

Avoid starting variable names with a number, this will avoid the variable being mis-interpreted as a parameter
%123_myvar% < > %1 23_myvar

To display undocumented system variables:

SET "Arithmetic expressions (SET /a)

The expression to be evaluated can include the following operators:

Multiply *
Divide /
Add +
Subtract -
Modulus %
OR |
LSH <<
RSH >>
Multiply Variable *=
Divide Variable /=
Add Variable +=
Subtract Variable -=
AND Variable &=
OR Variable |=
XOR Variable ^=
LSH Variable <<=
RSH Variable <<=Prompt for user input

SET /P variable=[PromptString] The /P switch allows you to set a variable equal to a line of input entered by the user.
The PromptString is displayed before the user input is read. The PromptString can be empty.

To place the first line of a file into a variable:

Set /P _MyVar=
Display variables

Type SET without parameters to display all the current environment variables.

Type SET with just a variable name to display that variable

SET _department

Alternatively use the ECHO command:

ECHO [%_department%]

The SET command invoked with a string (and no equal sign) will display a wildcard list of all matching variables
e.g. Display variables that begin with ''Pro'':
SET pro
Display variables that begin with an underscore ''_''


Storing a text string:

C:\>SET _department=Sales and Marketing
C:\>set _
_department=Sales and Marketing

One variable can be based on another, but this is not dynamic

C:\>set xx=fish
C:\>set yy=%xx% chips
C:\>set yy
yy=fish chips

C:\>set xx=sausage
C:\>set yy
yy=fish chips

C:\>set yy=%xx% chips
C:\>set yy
yy=sausage chips

SET can be CALLed allowing a variable substring to be evaluated:

SET start=10
SET length=9
SET string=The quick brown fox jumps over the lazy dog
CALL SET substring=%%string:~%start%,%length%%%
ECHO (%substring%) Deleting an environment variable

Type SET with just a variable name and an equals sign
For example:

SET _department=

To be sure there is no trailing space after the command use
(SET _department=)

Variable names can include Spaces

A variable can contain spaces and also the variable name itself may contain spaces, therefore the following assignment:
SET my var=MyText
will create a variable called "my var"

SET _var =MyText
will create a variable called "_var " - note trailing space

To avoid problems with extra spaces appearing in your output, issue SET statements in parentheses, like this

(SET _department=Some Text)
Alternatively you can do
SET "_department=Some Text"

Note: if you wanted to actually include a bracket in the variable you need to use an escape character.

The SET command will set ERRORLEVEL to 1 if the variable name is not
found in the current environment.
This can be detected using the IF ERRORLEVEL command

Using variables in a SET /a calculation

Enclose any logical expressions in "quotes"

Several calculations can be put on one line if separated with commas.

Any SET /A calculation that returns a fractional result will be rounded down to the nearest whole number.

For example:

SET /A _result=2+4

set /a _result=2+4, _amount -= 20

SET /A _result="2<<3"
(=2 Lsh 3 = binary 10 Lsh 3 = binary 10000 = decimal 16)

SET /A _result=5 %% 2
(=5/2 = 2 + 2 remainder 1 = 1)

SET /A _result=5
SET /A _result+=5
SET /A _result+=5

SET /A _result=7 && 6
(=binary 111 AND binary 110 = binary 110 = 6)SET /A will treat any character string in the expression as an environment variable name. This allows you to do arithmetic with environment variable values without having to type any % signs to get the values.

For example:

:: this will return 6

:: this will return 6

:: this will return 5
This last result demonstrates a minor bug present in NT 4 sp3.

Leading Zero will specify Octal

Numeric values are decimal numbers, unless prefixed by
0x for hexadecimal numbers,
0b for binary numbers and
0 for octal numbers.

So 0x12 is the same as 0b10010 is the same as 022.

The octal notation can be confusing - all numeric values that start with zeros are treated as octal but 08 and 09 are not valid numbers because 8 and 9 are not valid octal digits.

This is often a cause of error when performing date arithmetic. For example SET /a _day=07 will return the value=7, but SET /a _day=09 will return an error.

Permanent Changes

Changes made using the SET command are NOT permanent, they apply to the current CMD prompt only and remain only until the CMD window is closed.
To permanently change a variable at the command line use SetX
or in the GUI - Control Panel, System, Environment, System/User Variables

Changing a variable permanently with SetX will not affect any CMD prompt that is already open.
Only new CMD prompts will get the new setting.

You can of course use SetX in conjunction with SET to change both at the same time, but neither SET or SetX will affect other CMD sessions that are already running. When you think about it - this is a good thing.

It is also possible (although undocumented) to add permanent env variables to the registry [HKEY_CURRENT_USER\Environment]
(using REGEDIT)

System Environment variables can also be found in [HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]


Any SET statement in c:\autoexec.bat may be parsed at boot time
Variables set in this way are not available to 32 bit gui programs - they won''t appear in the control panel.
They will appear at the CMD prompt.

If autoexec.bat CALLS any secondary batch files, the additional batch files will NOT be parsed at boot.
This behaviour can be useful on a dual boot PC.

If Command Extensions are disabled all SET commands are disabled other than simple assignments like:

About the Author

FortyPoundHead has posted a total of 1974 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers