title: How do I add to the Windows PATH variable using setx? Having weird problems
body: <p>I want to modify the Windows PATH variable using <code>setx</code>. The following works at least 50% of the time on Windows 8:</p>
  <pre><code>setx PATH %PATH%;C:\Python27\;C:\Python27\Scripts\
  </code></pre>
  <p>If it gives the error &quot;the default argument can only be used 2 times&quot;, then the following works some of the time:</p>
  <pre><code>setx PATH &quot;%PATH%;C:\Python27\;C:\Python27\Scripts\&quot;
  </code></pre>
  <p>The difference is that we wrapped the second argument in quotes. I believe the quotes are necessary when <code>%PATH%</code> expands to include spaces.</p>
  <p>However, I have encountered some weird problems on Windows 7. On one particular Windows 7 machine, I had this problem:</p>
  <pre><code>echo %PATH%
  </code></pre>
  <p>It prints:</p>
  <pre><code>C:\Foo\;C:\Bar\;[...lots of stuff...]C:\Baz\
  </code></pre>
  <p>Then I do this:</p>
  <pre><code>setx PATH &quot;%PATH%;C:\Quux\&quot;
  </code></pre>
  <p>Then it says &quot;Error: Truncated at 1,024 characters.&quot; Now let's check what PATH contains:</p>
  <pre><code>echo %PATH%
  </code></pre>
  <p>It prints:</p>
  <pre><code>C:\Foo\;C:\Foo\;C:\Bar\;C:\Bar\;[...lots of stuff, now duplicated...]C:\B
  </code></pre>
  <p>...and it is cut off at 1,024 characters. It ran over because of the duplicates. Also interesting: The value of PATH changes despite the fact that <code>setx</code> raised an error and did not say &quot;Success&quot;.</p>
  <p>I was able to repeat this strange behavior several times (luckily I had saved the original contents of PATH).</p>
  <p>At the moment, the only surefire way I know to append to the PATH is the following:</p>
  <ol>
  <li><p><code>echo</code> the PATH.</p>
  </li>
  <li><p>Copy the contents of PATH into a text file and manually add <code>;C:\Python27\;C:\Python27\Scripts\</code> to the end of the PATH.</p>
  </li>
  <li><p>Copy the whole thing out of the text file.</p>
  </li>
  <li><p><code>setx PATH &quot;&lt;paste the string here&gt;&quot;</code></p>
  </li>
  </ol>
  <p>That process works every single time on both Windows 7 and Windows 8.</p>
  <p>I should really be able to do this in one command. What am I doing wrong?</p>