Home > Software engineering >  Powershell and correctly quoting paths for command line tools
Powershell and correctly quoting paths for command line tools

Time:08-22

I have this program, Search everything, its a search tool. It has a command line tool that can be used to invoke a search without manually interacting with the GUI interface.

Its manual states that double quotes need to be escaped as well as paths with spaces in them, Command-line Options:

Use """ to escape a literal "
For example, set the search to: "foo bar"
Everything64.exe -s """"foo bar""""
(Remember to also escape spaces with double quotes)

Paths with no spaces in them work just fine:

Everything64.exe -s c:\temp

Everything64.exe -s """C:\Program Files""""

The above attempt just inserts a scrambled mixture of my users home and program files paths

I attemped to escape the double quotes with a backtick for every double quote, this just replaces all double quotes with back slashes :

Everything64.exe -s `"`"`"C:\Program Files`"`"`"

The same command line tool works just fine in command prompt.

EDIT 1: My goal is to instead have "C:\Program Files". the program accepts its own parameters in the GUI search bar, thus it requires that if the path has a space in it, it be quoted. This way search like"C:\Program Files" !.jpg means all files in folder accept jpg files.


I have searched and searched and cant arrive at what is causing this. I much prefer to stay in powershell and avoid cmd, please help

I have taken the liberty to cross post this question on other forums as well.

PSVersion 7.3.0-preview.7

Windows Terminal Preview 1.15.2003.0

Any help would be greatly appreciated!

CodePudding user response:

In order for an external program such as Everything64.exe to see """C:\Program Files""" on its command line, use the following, as of PowerShell 7.2.x:

Everything64.exe -s '"""C:\Program Files"""'

If you need to use "..." quoting in order to use string interpolation:

Everything64.exe -s "`"`"`"C:\Program Files`"`"`""

Note:

  • This shouldn't work, but - serendipitously, in this case - does as of PowerShell 7.2.x, due to a long-standing bug, where " embedded in arguments passed to external programs aren't properly escaped - see this answer.

  • Thus, depending on how this bug is fixed in a future version, the above may break.

A future-proof - but limited - alternative is to use --%, the stop-parsing token instead:

Everything64.exe -s --% """C:\Program Files"""

However, --% has fundamental limitations, notably not being able to incorporate PowerShell variables and expression into the command - see this answer.

  • Related