如何在 powershell -Command ... 中使用管道 (|)?

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

我想从 Windows 控制台命令行执行 powershell 命令。我的方法是:

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

问题:

如果复杂的 powershell 命令包含 powershell 管道运算符,那么 Windows 控制台似乎会解释它,而不是将其作为参数逐字传递给 powershell 可执行文件。

例如:

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

给出标准控制台错误消息"'anything' 未被识别为内部或外部命令" 可运行的程序或批处理文件。

问题

如何解决这个问题?

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

由于|在cmd中有类似的功能,需要转义

在 cmd 中,合适的转义序列是 ^|:

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