CINXE.COM
Set - Environment Variable - Windows CMD - SS64.com
<!doctype html><html lang="en"> <head><meta charset="UTF-8"> <meta name="viewport" content="width=768, initial-scale=1"> <link rel="stylesheet" href="../main.css" type="text/css"> <link rel="icon" href="/favicon.ico" sizes="48x48"> <link rel="icon" href="/favicon.svg" sizes="any" type="image/svg+xml"> <title>Set - Environment Variable - Windows CMD - SS64.com</title> <link rel="canonical" href="https://ss64.com/nt/set.html"> <script async src="/js/q.js"></script> <script async src="/js/content.js"></script> </head><body><!-- #BeginLibraryItem "/Library/head_nt.lbi" --><div id="tnav"><ul> <li class="tbtn"><a href="../">SS64</a></li> <li class="tbtn"><a href="../nt">CMD <svg viewBox="0 0 13 20.1" width="13" height="13" style="transform: rotate(0deg);"><path d="M0 2.9l7.2 7.2-7.1 7.1L3 20.1l7.1-7.1 2.9-2.9L2.9 0 0 2.9"></path></svg></a></li> <li class="tbtn"><a href="syntax.html">How-to <svg viewBox="0 0 13 20.1" width="13" height="13" style="transform: rotate(0deg);"><path d="M0 2.9l7.2 7.2-7.1 7.1L3 20.1l7.1-7.1 2.9-2.9L2.9 0 0 2.9"></path></svg></a></li> <li><div id="search"><form action="https://www.google.com/search" method="get"> <input type="text" name="q" id="qu" size="27" maxlength="255" aria-label="Search text"> <input class="submit mousetrap" value="Search" id="btn" type="submit"> <input type="hidden" name="sitesearch" value="ss64.com/nt/"><input type="hidden" name="udm" value="14"></form></div></li> </ul></div><!-- #EndLibraryItem --><h1>SET</h1> <p>Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.</p> <pre>Syntax SET <i>variable</i> SET <i>variable</i>=<i>string</i> SET "<i>variable</i>=<i>string</i>" SET "<i>variable</i>=" SET /A <i>"variable</i>=<i>expression</i>" SET /P <i>variable</i>=[<i>promptString</i>] SET " Key <i>variable</i> A new or existing environment variable name e.g. _num <i>string</i> A text string to assign to the variable. /A Arithmetic expression see full details <a href="#expressions">below</a>. /P Prompt for user input.</pre> <p>Variable names are not case sensitive but the contents can be.</p> <p>It is good practice to avoid using any <a href="syntax-esc.html#delimiters">delimiter</a> characters (spaces, commas etc) in the variable <i>name</i>.</p> <p>Delimiter characters can be used in the <i>value</i> if the complete assignment is surrounded with double quotes to prevent the delimiter being interpreted.</p> <p>Any extra spaces around either the variable name or the <i>string</i>, will <b>not</b> be ignored, SET is not forgiving of extra spaces like many other scripting languages. So use <span class="code">SET alpha=beta</span>, not <span class="code">SET alpha = beta</span></p> <p>The first character of the name must <a href="syntax-percent.html">not be numeric</a>. It is a common practice to prefix variable names with either an underscore or a dollar sign <span class="code">_variable</span> or <span class="code">$variable</span>, these prefixes are not required but help to prevent any confusion with the standard built-in <a href="syntax-variables.html">Windows Environment variables</a> or any other command strings.</p> <p><span class="code">SET PATH</span>... the command <span class="code">SET PATH</span> is functionaly identical to the <a href="path.html"><span class="code">PATH</span></a> command for modifying the user’s PATH variable, see the <a href="path.html"><span class="code">PATH</span></a> page for more detail.</p> <p>The CMD shell will fail to read an environment variable if it contains more than <a href="https://support.microsoft.com/help/830473/">8,191</a> characters.</p> <p>SET is an <a href="syntax-internal.html">internal</a> command.<br> If <a href="cmd.html">Command Extensions</a> are disabled all SET commands are disabled other than simple assignments like:<span class="code"> _variable=MyText</span></p> <h2>Display a variable:</h2> <blockquote> <p>In most contexts, surround the variable <i>name</i> with <span class="code">%</span>’s and the variable’s <i>value</i> will be used<br> e.g. To display the value of the<span class="code"> _department</span> variable with the ECHO command: <span class="code"><br> ECHO %_department%</span></p> <p>If the variable name is not found in the current environment then SET will set %ERRORLEVEL% to 1 .<br> This can be detected using <a href="if.html">IF</a> ERRORLEVEL ...</p> <p>Including extra characters can be useful to show any white space:<br> <span class="code">ECHO [%_department%</span>]<br> <span class="code">ECHO "%_department%</span>"</p> <p>Type SET without parameters to display all the current environment variables.<br><br> Type SET with a variable name to display that variable <span class="code"><br> SET _department</span><br><br> The SET command invoked with a string (and no equal sign) will display a wildcard list of all matching variables<br><br> Display variables that begin with 'P': <span class="code"><br> SET p</span><br> Display variables that begin with an underscore <span class="code"><br> SET _ </span></p> </blockquote> <h2>Set a variable:</h2> <blockquote> <p>Example of storing a text string:<br><br> <span class="code">C:\> SET "_dept=Sales and Marketing"<br> C:\> set _<br> _dept=Sales and Marketing</span></p> <p>Set a variable that contains a <a href="syntax-redirection.html">redirection</a> character, note the position of the quotes which are not saved:</p> <p class="code">SET "_dept=Sales & Marketing"</p> <p>One variable can be based on another, but this is not dynamic<br> E.g.</p> <p class="code">C:\> set "xx=fish"<br> C:\> set "msg=%xx% chips"<br> C:\> set msg<br> msg=fish chips</p> <p class="code">C:\> set "xx=sausage"<br> C:\> set msg<br> msg=fish chips</p> <p class="code">C:\> set "msg=%xx% chips"<br> C:\> set msg<br> msg=sausage chips</p> <p>Avoid starting variable names with a number, this will avoid the variable being mis-interpreted as a <a href="syntax-args.html">parameter</a><br> <span class="code">%123_myvar% < > %1 23_myvar</span><br><br> To display undocumented system variables:</p> <p class="code">SET "</p> </blockquote> <h2>Values with Spaces - using Double Quotes</h2> <blockquote> <p>There is no hard requirement to add quotation marks even when assigning a value that includes spaces:</p> <p class="code">SET _variable=one two three<br> ECHO %_variable%</p> <p>Adding quotation marks is a best practise and will account for any trailing spaces and special characters like <span class="code">'&'</span>.<br> The variable contents will <b>not </b>include the surrounding quotes:</p> <p class="code">SET "<b>_variable=one & two</b>"<br> ECHO "%_variable%"</p> <p>If you place quotation marks around just the value, then those quotes <b>will</b> be stored:</p> <p class="code">SET _variable=<b>"one & two"</b><br> ECHO %_variable%</p> <p>This can be used for long filenames:</p> <p class="code">SET _QuotedPath=<b>"</b>H:\Config files\config 64.xml<b>"</b><br> COPY %_QuotedPath% C:\Demo\final.xml</p> <p>Alternatively you can add quotes at the point where they are needed:</p> <p class="code">SET<b> "</b>_Filename=config 64.xml"<br> COPY "H:\Config files\%_Filename%" C:\Demo\final.xml</p> </blockquote> <h2>Variable names with spaces</h2> <blockquote> <p>A variable can contain spaces and also the variable name itself can contain spaces, therefore the following assignment:<br> <span class="code">SET _var =MyText</span><br> will create a variable called <span class="code">"_var "</span> ← note the trailing space.</p> </blockquote> <h2>Prompt for user input</h2> <blockquote> <p>The <span class="code">/P</span> switch allows you to set a variable equal to a line of input entered by the user.<br> The Prompt string is displayed before the user input is read.</p> <pre>@echo off Set /P _ans=Please enter Department: || Set _ans=NothingChosen :: remove &’s and quotes from the answer (via string <a href="syntax-replace.html">replace</a>) Set _ans=%_ans:&=% Set _ans=%_ans:"=%<br>If "%_ans%"=="NothingChosen" goto sub_error<br>If /i "%_ans%"=="finance" goto sub_finance<br>If /i "%_ans%"=="hr" goto sub_hr goto:eof :sub_finance echo You chose the finance dept goto:eof :sub_hr echo You chose the hr dept<br>goto:eof :sub_error echo Nothing was chosen</pre> <p>Both the Prompt string and the answer provided can be left empty. If the user does not enter anything (just presses return) then the variable will be unchanged and the <a href="errorlevel.html">errorlevel</a> will be set to 1.</p> <p>The script above strips out any '&' and " characters but may still break if the string provided contains both.<br> For user provided input, it is a good idea to fully sanitize any input string for potentially problematic characters (unicode/smart quotes etc).</p> <p>The <a href="choice.html">CHOICE</a> command is an alternative for user input, CHOICE accepts only one character/keypress, when selecting from a limited set of options it will be faster to use.</p> </blockquote> <h2>Echo a string with no trailing CR/LF</h2> <blockquote> <p>The standard ECHO command will always add a CR/LF to the end of each string displayed, returning the cursor to the start of the next line.<br> SET /P does not do this, so it can be used to display a string. Feed a NUL character into SET /P like this, so it doesn’t wait for any user input:</p> <p class="code">Set /P _scratch="This is a message to the user " <nul</p> </blockquote> <h2>Place the first line of a file into a variable:</h2> <blockquote> <p class="code">Set /P _MyVar=<MyFilename.txt<br> Echo %_MyVar%</p> <p>The second and any subsequent lines of text in the file will be discarded.</p> <p>In very early versions of CMD, any carriage returns/new lines (CR+LF) before the first line containing text were ignored.</p> </blockquote> <h2>Delete a variable</h2> <blockquote> <p>Type SET with just the variable name and an equals sign:<br><br> <span class="code">SET _department=</span><br><br> Better still, to be sure there is no trailing space after the = place the expression in parentheses or quotes:<br> <span class="code">(SET _department=)<br> or<br> SET "_department="</span></p> </blockquote> <h2><a name="expressions" id="expressions"></a>Arithmetic expressions (SET /a)</h2> <blockquote> <p>Placing expressions in "quotes" is optional for simple arithmetic but required for any expression using logical operators or parentheses.<br> A best practice is to use quotes around all SET /A expressions, then they will always be in place when needed.</p> <p>When referring to a variable in your expression, SET /A allows you to omit the %’s so <span class="code">_myvar</span> instead of <span class="code">%_myvar%</span></p> <p>The expression to be evaluated can include the following operators:<br> For the Modulus operator use (%) on the command line, or in a batch script it must be <a href="syntax-percent.html">doubled up</a> to (%%) as below.<br> This is to distinguish it from a <a href="for.html">FOR</a> parameter.</p> <pre> + Add set /a "_num=_num+5" += Add variable set /a "_num+=5" - Subtract set /a "_num=_num-5" -= Subtract variable set /a "_num-=5" * Multiply set /a "_num=_num*5" *= Multiply variable set /a "_num*=5" / Divide set /a "_num=_num/5" /= Divide variable set /a "_num/=5" %% <a href="https://en.wikipedia.org/wiki/Modular_arithmetic">Modulus</a> set /a "_num=17%%5" %%= Modulus set /a "_num%%=5" ! Logical negation 0 (FALSE) ⇨ 1 (TRUE) and any non-zero value (TRUE) ⇨ 0 (FALSE) ~ Bitwise invert & AND set /a "_num=5&3" 0101 <a href="https://www.guru99.com/c-bitwise-operators.html">AND</a> 0011 = 0001 (decimal 1) &= AND variable set /a "_num&=3" | OR set /a "_num=5|3" 0101 <a href="https://www.guru99.com/c-bitwise-operators.html">OR</a> 0011 = 0111 (decimal 7) |= OR variable set /a "_num|=3" ^ XOR set /a "_num=5^3" 0101 <a href="https://www.guru99.com/c-bitwise-operators.html">XOR</a> 0011 = 0110 (decimal 6) ^= XOR variable set /a "_num=^3" << Left <a href="https://www.bbc.co.uk/bitesize/guides/z6qqmsg/revision/4">Shift</a>. (sign bit ⇨ 0) An arithmetic shift. >> Right <a href="https://www.bbc.co.uk/bitesize/guides/z6qqmsg/revision/4">Shift</a>. (Fills in the sign bit such that a negative number always remains negative.) Neither ShiftRight nor ShiftLeft will detect overflow. <<= Left Shift variable set /a "_num<<=2" >>= Right Shift variable set /a "_num>>=2" ( ) Parenthesis group expressions set /a "_num=(2+3)*5" , Commas separate expressions set /a "_num=2,_result=_num*5" </pre> <p>Any SET /A calculation that returns a fractional result will be rounded down to the nearest whole integer.</p> <p>Floating point arithmetic is not supported but you can call <a href="../ps/powershell.html">PowerShell</a> for that: <span class="code">powershell.exe 12.9999999 + 2105001.01</span><br> or in a batch file: <span class="code"><br> For /F %%G in ('powershell.exe 12.9999999 + 2105001.01') do Echo Result: %%G</span></p> <p>If a variable name is specified as part of the expression, but is not defined in the current environment, then SET /a will use a value of 0.</p> <p>SET /A arithmetic shift operators do not detect overflow which can cause problems for any non-trivial math, e.g. the bitwise invert often incorrectly reverses the + / - sign of the result.<br><br> See <a href="#expressions">SET /a examples</a> below and <a href="../forum/set.html">this forum thread</a> for more.<br> also see <a href="setx.html">SetX</a>, <a href="syntax-replace.html">VarSearch</a> and <a href="syntax-substring.html">VarSubstring</a> for more on variable manipulation.</p> <p>SET /A should work within the full range of 32 bit signed integer numbers (-2,147,483,648 through 2,147,483,647) but in practice for negative integers it will not go below -2,147,483,647 because the correct two’s complement result 2,147,483,648 would cause a positive overflow.</p> <h3><a id="e" tabindex="-1"></a>Examples</h3> <p class="code">SET /A "_result=2+4"<br> (=6)<br><br> SET /A "_result=5"<br> (=5)<br> SET /A "_result<b>+=</b>5"<br> (=10)<br><br> SET /A "_result=2<b><<</b>3"<br> (=16) { 2 Lsh 3 = binary 10 Lsh 3 = binary 10000 = decimal 16 }<br><br> SET /A "_result=5<b>%%</b>2"<br> (=1) { 5/2 = 2 + 2 remainder 1 = 1 }<br><br> SET /A "_var1=_var2=_var3=10"<br> (sets 3 variables to the same value - undocumented syntax.)</p> <p>SET /A will treat any character string in the expression as an environment variable name. This allows you to do arithmetic with environment variables without having to type any % signs to get the values. <span class="code">SET /A "_result=5 + _MyVar</span>"</p> <p>Multiple calculations can be performed in one line, by separating each calculation with commas, for example:</p> <p class="code">Set "_year=1999"<br> Set /a "_century=_year/100, _next=_century+1"</p> <p>The numbers must all be within the range of 32 bit signed integer numbers (-2,147,483,648 through 2,147,483,647) to handle larger numbers use <a href="/ps/">PowerShell</a> or <a href="/vb/">VBScript</a>.</p> <p>You can also store a math expression in a variable and substitute in different values, rather like a <a href="syntax-macros.html">macro</a>.</p> <p class="code">SET "_math=(#+6)*5"<br> SET /A _result="%_math:#=4%<br> Echo %_result%<br> SET /A _result="%_math:#=10%<br> Echo %_result%</p> </blockquote> <h2>Leading Zero will specify Octal</h2> <blockquote> <p>Numeric values are decimal numbers, unless prefixed by<br> <b class="code">0x</b> for hexadecimal numbers,<br> <b class="code">0 </b>for octal numbers.<br><br> So <span class="code">0x10</span> = <span class="code">020</span> = <span class="code">16</span> decimal<br><br> The octal notation can be confusing - all numeric values that start with zeros are treated as octal but 08 and 09 are not valid octal digits.<br> For example <span class="code">SET /a "_month=07</span>" will return the value 7, but <span class="code">SET /a "_month=09"</span> will return an error.</p> </blockquote> <h2>Permanent changes</h2> <blockquote> <p>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.<br> To permanently change a variable at the command line use <a href="setx.html">SetX</a><br> or with the GUI: Control Panel ➞ System ➞ Environment ➞ System/User Variables<br><br> Changing a variable permanently with SetX will not affect any CMD prompt that is already open.<br> Only new CMD prompts will get the new setting.<br><br> You can of course use SetX in conjunction with SET to change both at the same time:</p> <p class="code">Set _Library=T:\Library\<br> SetX _Library T:\Library\ /m</p> </blockquote> <h2>Change the environment for other sessions</h2> <blockquote> <p>Neither SET nor SetX will affect other CMD sessions that are already running on the machine. This as a good thing, particularly on multi-user machines, your scripts won’t have to contend with a dynamically changing environment while they are running.</p> <p>It is possible to add permanent environment variables to the registry (<span class="code">HKCU\Environment</span>), but this is an undocumented (and likely unsupported) technique and still it will not take effect until the users next login.</p> <p>System environment variables can be found in the registry here:<br> <span class="code"> HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment</span></p> </blockquote> <h2>CALL SET</h2> <blockquote> <p>The <span class="code">CALL SET</span> syntax will expand any variables passed on the same line, which is useful if you need to set one variable based on the value of another variable. CALL SET can also evaluate a <a href="syntax-substring.html">variable substring</a>, the <a href="call.html">CALL</a> page has more detail on this technique, though in many cases a faster to execute solution is to use <span class="code">Setlocal <a href="delayedexpansion.html">EnableDelayedExpansion</a></span>.</p> </blockquote> <h2>Autoexec.bat</h2> <blockquote> <p>Any SET statement in c:\autoexec.bat will be parsed at boot time<br> Variables set in this way are not available to 32 bit gui programs - they won’t appear in the control panel.<br> They will appear at the CMD prompt.<br><br> If autoexec.bat CALLS any secondary batch files, the additional batch files will NOT be parsed at boot.<br> This behaviour can be useful on a dual boot PC.</p> </blockquote> <h2>Errorlevels</h2> <blockquote> <p>When <a href="cmd.html">CMD Command Extensions</a> are enabled (the default):</p> <table> <caption> </caption> <tr> <th>Event</th> <th>Errorlevel</th> </tr> <tr> <td>If the variable was successfully changed.</td> <td><span class="code"><a href="errorlevel.html">Errorlevel</a> = <i>unchanged</i></span>, </td> </tr> <tr> <td><span class="code">SET</span> No variable found or invalid name.<br> <span class="code">SET _var=value</span> when _var name starts with "/" and not enclosed in quotes.<br> <span class="code">SET /P</span> Empty response from user.</td> <td class="code">1</td> </tr> <tr> <td><span class="code">SET /A</span> Unbalanced parentheses</td> <td class="code">1073750988</td> </tr> <tr> <td><span class="code">SET /A</span> Missing operand</td> <td class="code">1073750989</td> </tr> <tr> <td><span class="code">SET /A</span> Syntax error</td> <td class="code">1073750990</td> </tr> <tr> <td><span class="code">SET /A</span> Invalid number</td> <td class="code">1073750991</td> </tr> <tr> <td><span class="code">SET /A</span> Number larger than 32-bits</td> <td class="code">1073750992</td> </tr> <tr> <td><span class="code">SET /A</span> Division by zero</td> <td class="code">1073750993</td> </tr> </table> <p>If the Errorlevel is <i>unchanged</i>, typically it will be <span class="code">0</span> but if a previous command set an errorlevel, that will be preserved (this is a bug).</p> </blockquote> <p class="quote"># I got my mind set on you<br> # I got my mind set on you... - Rudy Clark (<a href="https://www.youtube.com/watch?v=k68Fob0QA_k">James Ray</a>/<a href="https://www.youtube.com/watch?v=_71w4UA2Oxo">George Harrison</a>)</p> <h3>Related commands</h3> <p class="space"><a href="syntax-substring.html">Syntax - VarSubstring</a> Extract part of a variable (substring).<br> <a href="syntax-replace.html">Syntax - VarSearch</a> Search & replace part of a variable.<br> <a href="syntax-variables.html">Syntax - Environment Variables</a> - List of default variables.<br> <a href="call.html">CALL</a> - Evaluate environment variables.<br> <a href="choice.html">CHOICE</a> - Accept keyboard input to a batch file.<br> <a href="endlocal.html">ENDLOCAL</a> - End localisation of environment changes, use to return values.<br> <a href="exit.html">EXIT</a> - Set a specific ERRORLEVEL.<br> <a href="path.html">PATH</a> - Display or set a search path for executable files.<br> <a href="reg.html">REG</a> - Read or Set Registry values.<br> <a href="setlocal.html">SETLOCAL</a> - Begin localisation of environment variable changes.<br> <a href="setx.html">SETX</a> - Set an environment variable permanently.<br> <a href="syntax-args.html">Parameters</a> - get a full or partial pathname from a command line variable.<br> <a href="https://stackoverflow.com/questions/6379619/explain-how-dos-batch-newline-variable-hack-works">StackOverflow</a> - Storing a Newline in a variable.<br> Equivalent PowerShell: <a href="../ps/set-variable.html">Set-Variable</a> - Set a variable and a value (set/sv).<br> Equivalent PowerShell: <a href="../ps/read-host.html">Read-Host</a> - Prompt for user input.<br> Equivalent bash command (Linux): <a href="../bash/env.html">env</a> - Display, set, or remove environment variables.</p><!-- #BeginLibraryItem "/Library/foot_nt.lbi" --><!-- AuctionX Display platform tag START --> <div id="27677x300x250x4627x_ADSLOT1" clickTrack="%%CLICK_URL_ESC%%"></div> <script async src="https://served-by.pixfuture.com/www/delivery/headerbid.js" slotId="27677x300x250x4627x_ADSLOT1" refreshTime="5" refreshInterval="120"></script><!-- AuctionX Display platform tag END --> <hr> <div id=bl> </div> <div id=br>Copyright © 1999-2025 <a href="https://ss64.com/">SS64.com</a><br> Some rights reserved</div><!-- #EndLibraryItem --></body> </html>