Conditionals
Conditionals enable execution of code based on provided conditions. They take in an expression and check if it is True
or False
. If the expression is True
, one set of statements is executed. Otherwise, another set of instructions is performed. This control of flow makes programs more robust by enabling them to address multiple scenarios.
There are four types of conditional statements in PowerShell:
if
else
elseif
switch
if
The if
statement executes a block of code if the condition expression is True
.
$my_num = 2if($my_num -eq 2){Write-Host "A True Statement"}Write-Host "After the if statement"
The example above prints "A True Statement"
and "After the if statement"
if $my_num
is equal to 2
. Otherwise, it skips the statement within the if
curly braces { }
and just prints "After the if statement"
.
else
The else
clause always follows the if
statement.
- If the condition is
True
, the code in theif
section is executed. - If the condition is
False
, the code in theelse
section is executed.
$my_num = 2if($my_num -gt 0) {# Prints if $my_num is more than 0Write-Host "A positive number"}else {# Prints if $my_num is less than or equal to 0Write-Host "A negative number"}
elseif
One or more elseif
statements can be added between if
and else
to test multiple conditions.
$my_num = 2if($my_num -gt 0) {# Prints if $my_num is more than 0Write-Host "A positive number"}elseif ($my_num -lt 0) {# Prints if $my_num is less than 0Write-Host "A negative number"}else {# Prints if both of the previous conditions failWrite-Host "Zero"}
switch
The switch
statement provides a simpler syntax for the same behavior as if
/ elseif
expressions that check for equality.
$my_num = 2switch ($my_num) {1 {Write-Host "You chose 1"}2 {Write-Host "You chose 2"}3 {Write-Host "You chose 3"}default {Write-Host "No match"}}
The example above prints "You chose 2"
. If $my_num
was 4
, for example, it would print "No match"
since the default
clause is run if all other conditions fail.
PowerShell allows conditional expressions to be used in switch
statements.
$my_num = 10switch ($my_num) {{$PSItem -gt 5} {Write-Host "Greater than 5"}{$PSItem -gt 0} {Write-Host "Greater than 0"}{$PSItem -lt 5} {Write-Host "Less than 5"}}
Note:
$PSItem
is an automatic variable that contains the value passed to theswitch
statement. It can be replaced with its shorthand alias$_
.
The above example prints "Greater than 5"
and "Greater than 0"
since their corresponding conditions are True
. Even if a condition is True
, the following conditions are still tested unless a break
statement is provided, as in the example below which only prints "Greater than 5"
.
$my_num = 10switch ($my_num) {{$_ -gt 5} {Write-Host "Greater than 5"break}{$_ -gt 0} {Write-Host "Greater than 0"}{$_ -lt 5} {Write-Host "Less than 5"}}
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PowerShell on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn PowerShell
Jump into PowerShell through interactive lessons on variables, operators, control flow, objects, arrays, and functions.Beginner Friendly6 hours