title: how to debug mixed cmd and powershell scripts to solve special character problems like ampersand &
body: <p>I am creating a dos batch script (cmd.exe) <code>.cmd</code> which through powershell elevates itself as administrator.</p>
  <p>But if it's stored in a folder that contains both spaces and ampersand in its name, it won't work.</p>
  <p>I am not an expert so I searched the web, but I could not find a solution or documentation to solve the problem. So I would also like to know how to debug this particular situation and the possible solutions to the problem and where to find the documentation describing the solution or the steps to get there .. (if any).</p>
  <p>To recreate the problem: I created a folder under the desktop named &quot;Pie &amp; tea&quot; without quotes. And in the folder I created a script &quot;test.cmd&quot; inside which I put only the essential to reproduce the problem. This is the content of the script:</p>
  <pre><code>@echo off
  
  pause
  
  PowerShell start &quot;%~f0&quot; -verb runas
  
  pause
  
  goto: eof
  </code></pre>
  <p>I run the script by double clicking on it (this is the desired mode). An error is shown with this code (I translate from italian with a translator):</p>
  <pre><code>Press any key to continue. . .
  In row: 1 car: 34
  + start C:\Users\fposc\Desktop\Pie &amp; tea\test.cmd -verb runas
  +                                  ~
  Ampersand (&amp;) not allowed. The &amp; operator is reserved for future use. Use &quot;&amp;&quot; to pass the ampersand as
  string.
      + CategoryInfo: ParserError: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId: AmpersandNotAllowed
  
  Press any key to continue. . .
  </code></pre>
  <p>And following some instructions I modified the line of code with powershell by tripling the double quotes:</p>
  <pre><code>PowerShell start &quot;&quot;&quot;%~f0&quot;&quot;&quot; -verb runas
  </code></pre>
  <p>But the cmd window opens and after pressing the space the UAC starts and after giving the consent <strong>another window appears for a moment but it closes immediately and I could not see any errors</strong>.</p>
  <p>I have seen on the web any solutions using single quotes <code>'</code> or the powershell escape character <code>`</code> or the caret <code>^</code> of the dos batch or the <code>--%</code> parameter of the powershell etc.
  But i was unable to resolve.
  I do not put the attempts made here because I copied in a stupid way (since I am not familiar with the mixed cmd / powershell operation) and I made numerous random attempts combining the various solutions. Without any positive outcome.</p>
  <p>I have also seen solutions that I would like to evaluate, such as:</p>
  <ol>
  <li>change directories and use relative paths.</li>
  <li>prepare string variables and replace characters.</li>
  <li>Use powershell -encodedcommand to pass parameters.</li>
  </ol>
  <blockquote>
  <blockquote>
  <blockquote>
  <p>But the preferred solution is the one that explains how to escape characters on the single line of code <code>PowerShell start &quot;%~f0&quot; -verb runas</code> and how to get there and the reference documentation.</p>
  </blockquote>
  </blockquote>
  </blockquote>
  <p>Some links visited:</p>
  <p><a href="https://github.com/microsoft/terminal/issues/2419" rel="nofollow noreferrer">VBS Shell.Application and PowerShell Start-Process do not escape ampersands internally</a></p>
  <p><a href="https://github.com/Azure/azure-cli/blob/dev/doc/quoting-issues-with-powershell.md" rel="nofollow noreferrer">Quoting issues with PowerShell</a></p>
  <p><a href="https://github.com/jorangreef/sudo-prompt/issues/97" rel="nofollow noreferrer">Issues on windows when the username contains &amp;</a></p>
  <p><a href="https://www.py4u.net/discuss/1304527" rel="nofollow noreferrer">Executing PowerShell script via explorer context menu on items containing ampersands in their names</a></p>
  <p><a href="https://stackoverflow.com/questions/33351455/batch-file-deal-with-ampersand-in-folder-name">Stackoverflow: batch file deal with Ampersand (&amp;) in folder name</a></p>
  <p><a href="https://stackoverflow.com/questions/12090312/how-to-access-file-paths-in-powershell-containing-special-characters">Stackoverflow: How to access file paths in PowerShell containing special characters</a></p>
  <p><a href="https://stackoverflow.com/questions/57699262/how-to-pass-strings-to-powershell-from-cmd-with-ampersands-in-values">Stackoverflow: How to pass strings to powershell from cmd with ampersands in values?</a></p>
  <hr />