mikemaccana mikemaccana - 2 years ago 183
PowerShell Question

How can I run a command with pipe character in its parameters via powershell?

I wish to run the command:

mocha -i -g 'database|network|skip'

With the pipe being part of the arguments to mocha. However powershell thinks 'network' is a program that I am trying to pipe to:

network : The term 'network' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path

was included, verify that the path is correct and try again.

Some research mentioned the
operator to stop Powershell parsing

The stop-parsing symbol (--%), introduced in Windows PowerShell 3.0,
directs Windows PowerShell to refrain from interpreting input as
Windows PowerShell commands or expressions.

however running:

mocha --% -i -g 'database|network|skip'

Still gives the same error. Which makes sense, since:

The stop-parsing symbol is effective only until the next newline or
pipeline character.

How can I run a command with a pipe symbol in powershell?

Edit: Image attached for person who refuses to believe me even after issue has been replicated, and answered by two people:

enter image description here

Answer Source

Using an example with find you will get similar results.

find /c "this|that|andtheotherthing" C:\temp\EventCombMT.txt

find : FIND: Parameter format not correct
At line:1 char:1
+ find /c "this|that|andtheotherthing" C:\temp\EventCombMT.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (FIND: Parameter format not correct:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Pretty sure this is because the quotes are consumed by the PowerShell interpreter and that leave your mocha command with an unquoted string. Doubling up the quotes is another was to prevent this.

find /c '"this|that|andtheotherthing"' C:\temp\EventCombMT.txt 

It seems this is not the case with mocha? In comments it was determined that we need to reverse the quote set from that seen in the find examples.

There are better ways to call external commands as you have seen in your linked post. Like using the call operator and a hashtable. You would still have to address the quotes though. You could escape a set of double quotes as well to get a similar effect.

find /c "`"this|that|andtheotherthing`"" C:\temp\EventCombMT.txt

For this to be correct though it does not really match the error you are getting. While I am correct about a solution I might be wrong about my interpretation of the issue. PowerShell should not care about the pipe character regardless of how it is quoted. That is what the quotes are for.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download