For PowerShell 5 (default install for Windows machines for the foreseeable future), you can of course use a semicolon to separate statements, but all statements will be executed by default even if one fails. Personally, I prefer to run things so that if one thing fails the whole line stops in the REPL and I imagine a lot of other folks do as well.
$ErrorActionPreference
lets you control the behavior of what happens when a statement fails but is a non-terminating error (which are most errors including command not found errors). You can set this variable $ErrorActionPreference="Stop"
in order to emulate the behavior of &&
in Bash and PowerShell 7 for the scope of this variable.
$ErrorActionPreference="Stop"
# Line break
fakeCommand; echo "Here"
I have had trouble finding precise documentation for this behavior, but this variable seems to be dynamically scoped so you can override it in a block temporarily if you don't want to set it globally.
Invoke-Command -ScriptBlock {$ErrorActionPreference="Stop"; fakeCommand; echo "Here"}
Finally, if you want something reusable, you can use this higher order function.
function Run-Block-With-Error($block) {
$ErrorActionPreference="Stop"
Invoke-Command -ScriptBlock $block
}
Which is then used as follows.
Run-Block-With-Error {fakeCommand; echo "Here"}
Note in the examples above that "Here" is not printed since fakeCommand
fails as it is not a real command.
I have tested the code provided in this solution for both PowerShell 5 and 7 and it should be fully portable, at least on Windows. While PowerShell 7 should be very similar on different platforms, I did not test these commands on Linux or MacOS.