Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access file paths in PowerShell containing special characters

Tags:

powershell

I am calling PowerShell from within a Java application (through the Windows command prompt) to read various file attributes.

For example,

powershell (Get-Item 'C:\Users\erlpm\Desktop\Temp\s p a c e s.txt').creationTime

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 -literalPath parameter:

powershell (Get-Item -literalpath 'C:\Users\erlpm\Desktop\Temp\brackets[].txt').creationTime

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 -literalPath 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 :-(

Any suggestions on how to pass a file path to PowerShell which may contain spaces, single quotes, square brackets, ampersands, dollar signs, etc.?

Someone here already showed me how to get it working from within PowerShell, but somehow the answer has been removed(?).

Anyway, I did create a file called $ & ' [].txt. This works form within PowerShell (needed to escape the &):

Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ & ' [].txt"


    Directory: C:\Users\erlpm\Desktop\Temp


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2012-08-23     14:22          0 $ & ' [].txt

But when I execute the same PowerShell command through the Windows command prompt, ...

powershell Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ & ' [].txt"

... I get this error:

Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
At line:1 char:55 \

  • Get-Item -LiteralPath C:\Users\erlpm\Desktop\Temp`$ & <<<< ' [].txt \
    • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException \
    • FullyQualifiedErrorId : AmpersandNotAllowed

Using the -command parameter and putting the PowerShell command between {} gives exactly the same error message ...

powershell -command {Get-Item -LiteralPath "C:\Users\erlpm\Desktop\Temp\`$ & ' [].txt"}
like image 858
jeroen_de_schutter Avatar asked Aug 23 '12 11:08

jeroen_de_schutter


2 Answers

This is really a question about cmd.exe string escaping, then. Combining cmd and PowerShell string escaping is a total nightmare. After quite a few attempts, I got your specific example to work:

powershell.exe -nologo -noprofile -command ^&{ dir -LiteralPath ^"""".\`$ & ' [].txt"" }

    Directory: C:\Users\latkin
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         8/23/2012   8:46 AM          0 $ & ' [].txt

Totally intuitive, right?

You can spend 15 minutes wrestling with escape sequences every time, or you can try some other approaches.

Put the file name in a text file, and read it out of there.

powershell.exe -command "&{dir -literal (gc .\filename.txt)  }"

or

Use a script

powershell.exe -file .\ProcessFiles.ps1  # In processfiles.ps1 you are in fully powershell environment, so escaping is easier

or

Use -EncodedCommand

See powershell.exe -? (last item shown) or http://dmitrysotnikov.wordpress.com/2011/07/06/passing-parameters-to-encodedcommand/

like image 137
latkin Avatar answered Oct 23 '22 13:10

latkin


This thread is 5 years old so maybe times have changed, but the current answer that worked for me was to use the backtick (`) symbol to escape the special character.

In my case, it was a dollar sign in a directory path that was failing. By putting a backtick before the dollar sign, everything worked.

Before:

$dir = "C:\folder$name\dir"  # failed

After:

$dir = "C:\folder`$name\dir" # succeeded
like image 39
Flat Cat Avatar answered Oct 23 '22 12:10

Flat Cat