Chapter 4: Conditional Statements
Master conditional statements in PowerShell, including if
, else
, and elseif
, to control the flow of your script based on specific conditions.
Conditional statements allow PowerShell scripts to make decisions and execute code based on certain conditions. This chapter covers how to use if
, else
, and elseif
statements to control the flow of your scripts, enabling more dynamic and responsive operations.
The If Statement
The if
statement is used to test a condition. If the condition evaluates to $true
, the code within the if
block is executed. Here�s the basic structure:
if (condition) {
# Code to execute if the condition is true
}
For example, the following code checks if a number is greater than 10:
$number = 15
if ($number -gt 10) {
Write-Output "The number is greater than 10"
}
Since $number
is 15, this condition is true, so the message will be displayed.
The Else Statement
The else
statement provides an alternative action if the if
condition is $false
. It runs when the if
condition is not met:
$number = 5
if ($number -gt 10) {
Write-Output "The number is greater than 10"
} else {
Write-Output "The number is 10 or less"
}
In this case, since $number
is 5, the else
block will execute and display the message �The number is 10 or less.�
The ElseIf Statement
The elseif
statement allows you to test multiple conditions in sequence. If the initial if
condition is $false
, PowerShell checks the elseif
conditions in order until one is $true
:
$number = 10
if ($number -gt 10) {
Write-Output "The number is greater than 10"
} elseif ($number -eq 10) {
Write-Output "The number is exactly 10"
} else {
Write-Output "The number is less than 10"
}
Here, since $number
is exactly 10, the elseif
condition will be met, and �The number is exactly 10� will be displayed.
Using Comparison Operators
PowerShell provides several comparison operators to test conditions in if
statements:
-eq
: Equal to-ne
: Not equal to-gt
: Greater than-lt
: Less than-ge
: Greater than or equal to-le
: Less than or equal to
These operators allow you to evaluate a wide range of conditions and make your scripts highly flexible.
Logical Operators for Complex Conditions
Logical operators can combine multiple conditions within an if
statement, making it possible to create complex decision-making structures:
-and
: Returns$true
if both conditions are true.-or
: Returns$true
if at least one condition is true.-not
: Reverses the condition�s result.
For example, to check if a number is between 5 and 15:
$number = 10
if ($number -ge 5 -and $number -le 15) {
Write-Output "The number is between 5 and 15"
}
Nested If Statements
You can place if
statements within other if
statements, known as nesting. This is useful for checking multiple conditions sequentially:
$age = 20
$hasPermission = $true
if ($age -ge 18) {
if ($hasPermission) {
Write-Output "Access granted."
} else {
Write-Output "Permission required."
}
} else {
Write-Output "Must be at least 18 years old."
}
In this example, access is granted only if both age and permission conditions are met.
The Switch Statement
The switch
statement provides an alternative to if
when you need to evaluate a variable against multiple values. This can make code cleaner when checking a single variable against various options:
$day = "Monday"
switch ($day) {
"Monday" { Write-Output "Start of the week" }
"Friday" { Write-Output "Almost weekend" }
default { Write-Output "Just another day" }
}
Here, if $day
is �Monday,� it outputs �Start of the week�; if �Friday,� it outputs �Almost weekend�; otherwise, it outputs �Just another day.�
Summary and Next Steps
In this chapter, we covered conditional statements in PowerShell, including if
, else
, elseif
, and switch
. We explored comparison and logical operators to help create flexible conditions. In the next chapter, we�ll look at loops and iteration, which allow you to automate repetitive tasks within your scripts.