Can I get “&&” or “-and” to work in PowerShell?

后端 未结 12 911
南笙
南笙 2020-11-29 17:33

&& is notoriously hard to search for on Google Search, but the best I\'ve found is this article which says to use -and.

Unfortunatel

12条回答
  •  青春惊慌失措
    2020-11-29 18:11

    In CMD, '&&' means "execute command 1, and if it succeeds, execute command 2". I have used it for things like:

    build && run_tests
    

    In PowerShell, the closest thing you can do is:

    (build) -and (run_tests)
    

    It has the same logic, but the output text from the commands is lost. Maybe it is good enough for you, though.

    If you're doing this in a script, you will probably be better off separating the statements, like this:

    build
    if ($?) {
        run_tests
    }
    

    2019/11/27: The &&operator is now available for PowerShell 7 Preview 5+:

    PS > echo "Hello!" && echo "World!"
    Hello!
    World!
    
    

提交回复
热议问题