PowerShell: Flow Control

By Xah Lee. Date: .

if statement

if (3 -gt 2) { Write-Output "yes" }

if else:

if (3 -gt 4) { Write-Output "yes" } else { Write-Output "no" }

multiple statements needs to be placed on separate lines, or with semicolon at the end.

if (3 -gt 4) { Write-Output "yes" } else {
Write-Output "no" ;
Write-Output "wrong";
 }

if elseif else chain:

$x = 9;
if ($x -eq 1) {Write-Output "is 1"}
elseif ($x -eq 2) {Write-Output "is 2"}
else {Write-Output "is other"}

if expression

if expression returns a value.

$y = ( 9 -gt 5 ? "red" : "green")
# $y is now green

PowerShell

How-to

Advanced

Script Examples