1

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?

7
  • 2
    Escape | with ^: Get-Something ^| Do-Something Commented Mar 10, 2021 at 15:02
  • 1
    Note that if you are running things directly from the commandline and not creating a commandline script of some sort, you can simply type powershell first and then you will be prompted with PS > prompt where you can enter any powershell commands you want natively without having to worry about escaping. Commented Mar 10, 2021 at 15:09
  • Why is your -Command value not surrounded in double quotes? It is a command string. Commented Mar 10, 2021 at 15:47
  • @Efie because it is an Azure provisioning extension, I will not there to "simply type powershell first" Commented Mar 10, 2021 at 15:49
  • @AdminOfThings Because the statements full of both double and single quotes themself Commented Mar 10, 2021 at 15:50

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

-1

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

Comments

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.