Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access file paths in Powershell containing special characters?
    primarykey
    data
    text
    <p>I am calling powershell from within a Java application (thru the Windows command prompt) to read various file attributes.</p> <p>e.g.</p> <pre><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><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, ... and all these characters seem to have a specific function in powershell for which the <em>-literalPath</em> parameter does not seem to work. 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><em><strong>Update:</em></strong> Someone here already showed me how to get it working from within Powershell, but somehow the answer has been removed ?<br> Anyway, I did create a file called <code>$ &amp; ' [].txt</code>. This works form <em>within</em> Powershell (needed to escape the &amp;) :</p> <pre><code> PS C:\Users\erlpm&gt; Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ &amp; ' [].txt" 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 thru the Windows command prompt, ...</p> <pre><code>C:\Users\erlpm&gt;powershell Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ &amp; ' [].txt" </code></pre> <p>... I get this error :</p> <pre><code> Ampersand not allowed. The &amp; operator is reserved for future use; use "&amp;" to pass ampersand as a string. At line:1 char:55 + Get-Item -LiteralPath C:\Users\erlpm\Desktop\Temp\`$ &amp; &lt;&lt;&lt;&lt; ' [].txt + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : AmpersandNotAllowed </code></pre> <p>Using the <code>-command</code> parameter and putting the powershell command between <code>{}</code> gives exactly the same error message ...</p> <pre><code>C:\Users\erlpm&gt;powershell -command {Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ &amp; ' [].txt"} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload