1

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

STOP 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

19
  • Can not reproduce this on v5.1 or v2. Commented Aug 30, 2016 at 13:48
  • @PetSerAl I can, reliably, on powershell 5.1.14393.82 Commented Aug 30, 2016 at 13:49
  • 2
    Can you provide minimal reproducible example? Commented Aug 30, 2016 at 13:50
  • 2
    Your example is not complete. It depends from mocha command, which can (and likely) be the sourse of error. Commented Aug 30, 2016 at 13:56
  • 1
    That is a cmd error and not a PowerShell one though. I was able to test the with find... hmmmm. Perhaps we need to reverse the quotes? mocha -i -g '"database|network|skip"' Commented Aug 30, 2016 at 14:33

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

No.. your right... I mixed up my errors with yours.
3

Thanks to PowerShell and external commands done right. You can quote the pipe character to stop powershell treating it as a pipe. In this case:

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

Works perfectly. As @matt has since mentioned, you can also run the (much neater):

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

2 Comments

I'm curious, would the same work the other way around "network'|'database'|'skip"
@Nkosi that would fail (and does fail in testing) since double quotes are not as strict as single quotes. See stackoverflow.com/questions/5967407/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.