简体   繁体   中英

How to use Pipe (|) in powershell -Command …?

I would like to execute powershell commands from a Windows console commandline. My method is:

c:\>powershell -Command mypowershellstatment1;mypowershellstatment1;etc

Issue:

In case if a complex powershell command contains powershell pipe operator, then it seems that Windows console interprets it, instead of passing it literally as parameter to the powershell executable.

For example:

c:\>powershell -Command mypowershellstatment1 | anything;mypowershellstatment1;etc

gives the standard console error message "'anything' is not recognized as an internal or external command" operable program or batch file.

Question

How to overcome this issue?

Since | has a similar function in cmd, you need to escape it.

In cmd, the appropriate escape sequence would be ^| :

C:\>powershell -Command Get-Something ^| Do-Something

powershell "command1 | command2 | command3... | commandN-1 | commandN"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM