title: How to access file paths in PowerShell containing special characters
body: <p>I am calling PowerShell from within a Java application (through the Windows command prompt) to read various file attributes.</p>
  <p>For example,</p>
  <pre class="lang-none prettyprint-override"><code>powershell (Get-Item 'C:\Users\erlpm\Desktop\Temp\s p a c e s.txt').creationTime
  </code></pre>
  <p>I am enclosing the file path in single quotes, because it may contain spaces.
  It worked fine, until I encountered a file path containing square brackets, which seem to be interpreted as a wildcard character. I managed to solve it by adding the <em>-literalPath</em> parameter:</p>
  <pre class="lang-none prettyprint-override"><code>powershell (Get-Item -literalpath 'C:\Users\erlpm\Desktop\Temp\brackets[].txt').creationTime
  </code></pre>
  <p>So far, so good... But file paths may also contain single quotes, dollar signs, ampersands, etc., and all these characters seem to have a specific function in PowerShell for which the <em>-literalPath</em> parameter does not seem to work.</p>
  <p>I tried enclosing the path with double quotes or escaping with the `character, but that did not solve my problem either :-(</p>
  <p>Any suggestions on how to pass a file path to PowerShell which may contain spaces, single quotes, square brackets, ampersands, dollar signs, etc.?</p>
  <p>Someone here already showed me how to get it working from within PowerShell, but somehow the answer has been removed(?).</p>
  <p>Anyway, I did create a file called <code>$ &amp; ' [].txt</code>.
  This works form <em>within</em> PowerShell (needed to escape the <code>&amp;</code>):</p>
  <pre class="lang-none prettyprint-override"><code>Get-Item -LiteralPath &quot;C:\Users\erlpm\Desktop\Temp\`$ &amp; ' [].txt&quot;
  
  
      Directory: C:\Users\erlpm\Desktop\Temp
  
  
  Mode                LastWriteTime     Length Name
  ----                -------------     ------ ----
  -a---        2012-08-23     14:22          0 $ &amp; ' [].txt
  </code></pre>
  <p>But when I execute the same PowerShell command through the Windows command prompt, ...</p>
  <pre class="lang-none prettyprint-override"><code>powershell Get-Item -LiteralPath &quot;C:\Users\erlpm\Desktop\Temp\`$ &amp; ' [].txt&quot;
  </code></pre>
  <p>... I get this error:</p>
  <blockquote>
  <p>Ampersand not allowed. The &amp; operator is reserved for future use; use &quot;&amp;&quot; to pass ampersand as a string. <br />
  At line:1 char:55 \</p>
  <ul>
  <li>Get-Item -LiteralPath C:\Users\erlpm\Desktop\Temp`$ &amp; &lt;&lt;&lt;&lt;  ' [].txt \
  <ul>
  <li>CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException \</li>
  <li>FullyQualifiedErrorId : AmpersandNotAllowed</li>
  </ul>
  </li>
  </ul>
  </blockquote>
  <p>Using the <code>-command</code> parameter and putting the PowerShell command between <code>{}</code> gives exactly the same error message ...</p>
  <pre class="lang-none prettyprint-override"><code>powershell -command {Get-Item -LiteralPath &quot;C:\Users\erlpm\Desktop\Temp\`$ &amp; ' [].txt&quot;}
  </code></pre>