user797963
user797963

Reputation: 3027

Passing parameter with a pipe in it into a powershell script - how do I escape the pipe?

Escaping chars in powershell is just silly. I've tried everything I can find to try and escape this pipe in a parameter that I'm passing into a powershell script, but I absolutely cannot escape it. It's nuts. I make sure to pass the param in single quotes, and have tried all of the powershell/windows escape chars (`,``,\,\,^,^^). Any help is greatly appreciated.

Example of test script:

$cmd = $Args[0] #set first parameter received to $cmd to run with Invoke-Expression (must wrap param in single quotes if it contains spaces)  
write-host $cmd

Calling test script with a pipe containing command:

C:\Windows\System32\cmd.exe /c powershell -executionPolicy Unrestricted -InputFormat none "D:\test_powershell\app\test_wrapper.ps1" 'test1 | test2'

Results:

'test2'' is not recognized as an internal or external command,
operable program or batch file.

Upvotes: 2

Views: 3775

Answers (2)

rismoney
rismoney

Reputation: 531

from cmd the escape character for the pipe would be the carat ^

Upvotes: 4

Joey
Joey

Reputation: 354526

A few notes:

  1. You don't need cmd. Just call PowerShell directly.
  2. -InputFormat can be either Text or XML. None is not a valid value here.
  3. Use the following (which is mentioned in the help too if escaping issues arise):

    powershell "&{ &'D:\test_powershell\app\test_wrapper.ps1' 'test 1 | test2' }"
    

Upvotes: 4

Related Questions