Codecademy Logo

Learn PowerShell: Control Flow

Control Flow in Powershell

Control Flow is the structure and ordering of conditional statements and loops to allow the script to execute in the desired fashion.

If statement

If statements test for expected conditions and define actions to take if those conditions are met.

This is a code snippet that shows the syntax for an if statement, that will execute if the condition of a variable $x is greater than 100.

if($x -gt 100){
write-host "This number is greater than 100"
}

ElseIf statements

ElseIf statements are used with if statements and should capture other specific conditions that are not met by the If statement.

This code snippet shows ElseIf statements relative to If and Else statements that take action based on the value of a variable $x.

if($x -eq 0){
write-host "Number is zero"
elseif(($x -gt 0) -and ($x -lt 6)){
write-host "Number is 1-5"
}
elseif(($x -gt 0) -and ($x -lt 10)){
write-host "Number is 6-10"
}
else{
write-host "Number is greater than 10"
}
}

Else statement

Else statements are used with if statements and should capture all other scenarios that are not met by the preceding If or ElseIf statements.

This code snippet shows an Else statement take yields a action based on the value of a variable $x not being met previously.

if($x -eq 0){
write-host "Number is zero"
}
elseif(($x -gt 0) -and ($x -lt 6)){
write-host "Number is 1-5"
}
elseif(($x -gt 0) -and ($x -lt 10)){
write-host "Number is 6-10"
}
else{
write-host "Number is greater than 10"
}

For Loop

For loops in PowerShell consist of initialization, condition, and increment, with the recursive action following.

This code snippet shows the syntax for a For loop, that will iterate through the loop ten times, using $i as the loop variable.

for($i = 0; $i -lt 10; $i++){
write-host $i
}

Foreach-Object loops

ForEach/ForEach-Object loops execute on all items in the object being piped to it.

The code snippet passes an object (the array $states, which is a list of all US States) to a Foreach loop, which then displays the name of each State as it iterates through.

$states | foreach-object{
write-host $_
}

While loops

While loops execute until a given condition is not met.

This code snippet shows a while loop that will execute as long as the variable $x is less than or equal to 5.

while($x -le 5){
write-host $x "is less than 5"
$x++
}

Do-While versus Do-Until

Do-While loops will execute at least once and continue to execute as long as a given condition is true, and it will exit upon a false condition. Do-Until loops will execute at least once and continue to execute as long as a given condition is false, and will exist upon a true condition.

This code snippet shows the subtle difference between the two loops, using when $x is equal to 5 as the condition. The Do-While will iterate as long as $x is less than or equal 5, whereas the Do-Until will iterate as long as $x is not greater than 5. Both would yield the same output.

$x = 0
Do{
write-host $x
$x++
while ($x -le 5)
------------------------------
$x = 0
Do{
write-host $x
$x++
}
until ($x -gt 5)
}

Break statement

The Break statement will exit the current loop and proceed with the rest of the code outside the loop.

This example uses a for loop to increment a variable $x from 1 to 10, but will break if/when $x is equal to 6.

for($i=0; $i -le 10; $i++){
write-host "Number is" $i
if($i -eq 6){ break }
}

Learn More on Codecademy