Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying something like:

set pwd = abc&123
echo password %pwd%

I get the result as...

'123' is not recognized as an internal or external command, operable program or batch file. 
password abc

Is the system like this or is there an escape for such characters?

share|improve this question

4 Answers 4

What you need to do is wrap your password in double quotes like so:

set pwd="abc&123"
echo %pwd%

Let me know how it goes.

share|improve this answer
Another option which may be preferable in some scenarios is set "pwd=abc&123" – Harry Johnston Feb 28 '13 at 2:57
-1 it is shocking that this has had 5 upvotes, it's wrong. Windows uses %var% or !var! you could've tested it yourself so easily. $var is in linux, if you echo $var in windows it just says literally $var – barlop May 12 at 11:25
@barlop: seeing as it's the other line that addresses the question, I don't see that it matters. – Harry Johnston May 18 at 1:42

I found that it works best with a combination of both martineau's answer and mastashake57's answer.

set pwd=abc^&123
echo %pwd%

Still fails, and

set pwd="abc&123"
echo %pwd%

Adds quotes to it (you can't remove the quotes with a for loop or string manipultaion because it will fail again, thanks to the ampersand), which isn't that great.


set "pwd=abc^&123"
echo %pwd%

Works perfectly.

Hope that helps.

share|improve this answer
+1. Be aware that this leaves the carat in the environment variable, i.e., it sets pwd to abc^&123. Most of the time this is probably exactly what you need to do, but it could catch you out if you're not aware of what's happening. – Harry Johnston Mar 10 '13 at 10:27
@HarryJohnston I must correct your spelling error. It's carEt. btw I know ntcmds.chm mentions it, but where in cmd /? documentation is it mentioned about the caret being the escape character? – barlop Mar 10 '13 at 11:52
@Barlop: so it is; I was confusing it with the homonym. As a general rule /? only provides a summary, not comprehensive documentation. – Harry Johnston Mar 10 '13 at 19:59
@barlop, ? The caret is the escape character. – Pacerier Aug 25 at 17:23
@Pacerier yes, caret is the escape character , nobody is disagreeing with that. and it is mentioned that it is the escape character, in ntcmds.chm(ntcmds.chm came with XP and is in an equivalent online… and maybe in some ways more up to date online - maybe more commands). I was just asking where in /? (if anywhere), it says that caret is the escape character – barlop Aug 25 at 18:09

From the command-line reference help file:

The ampersand (&), pipe (|), and parentheses ( ) are special characters that must be preceded by the escape character (^) or quotation marks when you pass them as arguments.

Seems like putting the whole password in quotation marks would be easier than escaping individual characters if there's more than two characters within it that need the treatment.

share|improve this answer
And quotes within quotes? – Pacerier Aug 25 at 17:17

What you really need is:

set pwd=abc^^^&123
echo %pwd%


set "pwd=abc^&123"
echo %pwd%


Since & is a special char, you must escape it with ^, resulting in ^&.

Running set pwd=abc^&123 will set the variable to the value abc&123. After that, if you were to run echo %pwd%, you are effectively running echo abc&123.

Yet echo abc&123 doesn't work as you expect, because & is a special char. You can avoid this by adding quotes: echo "abc&123" but your output would have the quote chars too. In other words, using quotes isn't exactly a proper solution.

What you need is to escape the & char when you run the echo command:

echo abc^&123

So the variable needs to contain the value abc^&123. To set the variable to the value abc^&123, you would need to escape the special chars ^ and &, resulting in:

set pwd=abc^^^&123
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.