19

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.

cxw
  • 16,685
  • 2
  • 45
  • 81
Luke Bakken
  • 8,993
  • 2
  • 20
  • 33

2 Answers2

10

Try this:

$errorActionPreference='Stop'; cmd1; cmd2
Ivan
  • 9,089
  • 4
  • 61
  • 74
6

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

conditional execution (&& and ||) in powershell

Community
  • 1
  • 1
EBGreen
  • 36,735
  • 12
  • 65
  • 85
  • 1
    I realized the silliness of saying "here is the same question already answered" as I posted this answer. I've voted to close this question. – EBGreen Jun 19 '12 at 15:29
  • 2
    This page serves a purpose; when searching for "chain commands powershell" you get here. So you can follow your link to the real answer. – andersand Sep 09 '13 at 06:26