Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chain commands on the PowerShell command line (like POSIX sh &&) [duplicate]

Does PowerShell have an equivalent to this command construct from sh (and derivatives):

$ cmd1 && cmd2

where cmd2 is only run when cmd1 exits sucessfully?

I know you can combine commands with a semicolon. However, this disregards the exit status of commands.

like image 665
Luke Bakken Avatar asked Jun 19 '12 15:06

Luke Bakken


People also ask

How do you chain two commands in PowerShell?

Using AND(&&) Operator If you want each command to be executed only if the previous one was successful, combine them with the && operator. You can use this in both CMD and Powershell. Usage: command1 && command2 && command3 [&& command4 ...]

What does && do PowerShell?

The && operator executes the right-hand pipeline, if the left-hand pipeline succeeded. Conversely, the || operator executes the right-hand pipeline if the left-hand pipeline failed.

How do I run a PowerShell script multiple times?

How to run a command multiple times in Windows PowerShell. Wrap your statement inside the curly braces of for ($i=1; $i -le n; $i++) { someCommand} , where n is a positive number and someCommand is any command. To access the variable (I use i but you can name it differently) you need to wrap it like this: $i .

What is CLS command in PowerShell?

The Clear-Host function removes all text from the current display, including commands and output that might have accumulated. When complete, it displays the command prompt. You can use the function name or its alias, cls .


2 Answers

Try this:

$errorActionPreference='Stop'; cmd1; cmd2
like image 188
Ivan Avatar answered Oct 29 '22 16:10

Ivan


There is no direct analog to && or ||. There are several discussions on alternatives though. Here is one example:

conditional execution (&& and ||) in powershell

like image 24
EBGreen Avatar answered Oct 29 '22 17:10

EBGreen