Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose my PATH is C:\WINDOWS\system32\;C:\Program Files\Important\


Results in a path of:

C:\WINDOWS\system32\;C:\Program Files\Important\;C:\My\Dir"

Notice the quotation mark at the end of the path. It's as though the backslash at the end of %NEW_PATH% escaped the final quote mark. I need the quotation marks because I have spaces in my path, but I don't want backslashes to be interpreted as escape characters.

What's the right way to include my PATH in the call to SETX?

share|improve this question
up vote 6 down vote accepted

Don't put a backslash at the end of %NEW_PATH%.

If you got random input to begin with instead of stuff you control, then you can do one of the following:

  • Leave out the closing quotation mark. This does work in some cases:

    > setx foo "bar baz
    > echo %foo%
    bar baz

    But doesn't do so well in others:

    > setx foo "Some stuff "with" embeddded quotes
    > echo %foo%
    Some stuff with embeddded quotes

    However, you have that problem already to begin with, so what you did there was dangerous at best, since it would destroy quoted paths (which you need if your path includes a semicolon).

    Which leads us to the following option:

  • Properly escape whatever goes into the command:

    set NEW_PATH=C:\I\Hate\User\Dirs\In\The\Root\
    rem Note the extra quotation marks; we're constructing the complete
    rem argument to setx here.
    rem The great escape
    set NEW_PATH_VAR=%NEW_PATH_VAR:"=\"%
    setx PATH %NEW_PATH_VAR%
share|improve this answer
That's great! (in an awful soul-sucking kind of way - I hate Windows batch-file programming). Thank you. – Jeremy Stein May 5 '10 at 16:20
@Jeremy: Just see it as kind of a better Brainfuck esolang. It's more fun that way :) – Joey May 7 '10 at 12:52

If NEW_PATH does not contain spaces, then you do not need to quote it:


If NEW_PATH does contain spaces, you could use the literal value, and again not quote the final backslash:

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.