How to escape pipe character from PowerShell command line to pass into non-PowerShell command

Question:

I’m in a PowerShell console session, trying to run a Gradle script that exists in the cwd from the PowerShell command line.

The Gradle command accepts an argument which includes quotes that can contain one or more pipe characters, and I can’t for the life of me figure out how to get PowerShell to escape it using just one line (or any number of lines, actually – but I’m not interested in multi-line solutions).

Here’s the command:

…which produces this error:

I’ve tried all of these variants, none of which work. Any idea?

Answer:

Well, I have found that:

First call your script like this as I first suggested:

Then modify your gradlew.bat script by adding quotes:

The problem is: now CMD_LINE_ARGS must be called within quotes or the same error will occur.

So I assume that your command line arguments cannot be something else and I’m handling each parameter one by one

The output is (for my mockup command):

The “=” has gone, because it has separated parameters from PowerShell. I suppose this won’t be an issue if your command has standard argument parsing.

Add as many arguments as you want, but limit it to 9 anyway.

Source:

How to escape pipe character from PowerShell command line to pass into non-PowerShell command by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply