In PowerShell, decision-making helps your script decide what to do based on certain conditions. The if, else, and elseif statements are key for this. Think of them as checkpoints where you check a condition (true or false) and then tell the script what to do next.
For example, you can use if to check if a file exists, else to do something if it doesn’t, and elseif to handle multiple conditions. These statements make your scripts more flexible, allowing them to respond to different situations and user inputs.
We’ll go through how to use them and how they can make your scripts smarter and more efficient.

If StatementAn if statement evaluates whether a condition is true or false. If it is, the function within the if block executes. If the condition is false, the code inside the block is ignored and the script moves on.
Syntax:
if (condition) {
# Code to execute if condition is true
}Example:
$age = 20
if ($age -ge 18) {
Write-Host "You are an adult."
}Here, the script checks if $age is greater than or equal to 18 and prints “You are an adult.”
Else StatementThe else statement comes after an if block and runs when the condition in the if statement is false. It lets you handle what should happen in the alternative scenario.
Syntax:
if (condition) {
# Code to execute if condition is true
} else {
# Code to execute if condition is false
}Example:
$age = 16
if ($age -ge 18) {
Write-Host "You are an adult."
} else {
Write-Host "You are not an adult."
}In this example, since $age is less than 18, the script will print “You are not an adult.”
ElseIf for Multiple ConditionsIf you need to check more than one condition, the elseif statement lets you evaluate additional conditions after an if statement. You can use multiple elseif statements to handle different possibilities, making your script more flexible.
Syntax:
if (condition1) {
# Code to execute if condition1 is true
} elseif (condition2) {
# Code to execute if condition2 is true
} else {
# Code to execute if none of the conditions are true
}Example:
$age = 16
if ($age -ge 18) {
Write-Host "You are an adult."
} elseif ($age -ge 13) {
Write-Host "You are a teenager."
} else {
Write-Host "You are a child."
}
Since $age is 16, the script will print “You are a teenager.”
PowerShell offers several comparison operators to check conditions. For numbers, you can use -eq (equal), -lt (less than), -gt (greater than), -ge (greater than or equal to) and more.
For strings, you can use -eq (equal) or -like (matches a pattern) to compare text. These operators help you create more precise conditions in your script.
Example:
$fruit = "apple"
if ($fruit -eq "apple") {
Write-Host "It's an apple!"
} else {
Write-Host "It's not an apple."
}Logical operators such as -and, -or, and -not can be used to combine numerous conditions in an if, elseif, or else block.
-and: Both conditions must be true.-or: At least one of the conditions must be true.-not: reverses (negates) the condition.These operators help you check more complex scenarios in a single statement.
Example:
$age = 20
$hasLicense = $true
if ($age -ge 18 -and $hasLicense) {
Write-Host "You are allowed to drive."
} else {
Write-Host "You are not allowed to drive."
}PowerShell also has a shorthand version of if-else called the ternary operator. It’s perfect for simple conditions and quick value assignments.
Syntax:
$variable = (condition) ? "value if true" : "value if false"Example:
$age = 25
$category = ($age -ge 18) ? "Adult" : "Minor"
Write-Host "You are an $category."You can nest if statements inside each other to check multiple layers of conditions. This helps you make more complex decisions
Example:
$age = 25
$hasID = $true
if ($age -ge 18) {
if ($hasID) {
Write-Host "You can enter the club."
} else {
Write-Host "You cannot enter the club without an ID."
}
} else {
Write-Host "You are too young to enter the club."
}Switch for Multiple ConditionsFor checking multiple conditions on a single variable, the switch statement is a cleaner and more readable alternative to using multiple if-else statements.
It lets you test a variable against several possible values without cluttering your code.
Example:
$day = "Monday"
switch ($day) {
"Monday" { Write-Host "Start of the workweek." }
"Friday" { Write-Host "End of the workweek!" }
default { Write-Host "Just another day." }
}You can check if a variable is $null (meaning it hasn’t been initialised or doesn’t have a value) in an if statement
Example:
$myVar = $null
if ($myVar -eq $null) {
Write-Host "Variable is null."
} else {
Write-Host "Variable is not null."
}throw for Error HandlingYou can use the throw keyword to stop script execution when a condition fails, making it great for validation or error handling. When you use throw, it triggers an error and halts the script.
Example:
$number = -5
if ($number -lt 0) {
throw "Error: Number cannot be negative."
} else {
Write-Host "Number is valid."
}PowerShell supports short-circuiting with the -and and -or operators. This means if the first condition is enough to determine the result, the second condition won’t be checked, saving time and resources.
Example:
$firstCheck = $false
$secondCheck = Write-Host "This will not print." # This line is never executed.
if ($firstCheck -and $secondCheck) {
Write-Host "Both checks passed."
} else {
Write-Host "At least one check failed."
}Yes, you can. PowerShell supports multiple elseif statements in the same script. They are evaluated in a top-to-bottom manner, and the first condition set to $true will run; everything else will be skipped out of the elseif statement.
You should use a switch when you want to compare a single expression against multiple possible values. It condenses the code, making it more readable and easier to maintain than if you had a long string of if-elseif statements.
Yes, with PowerShell 7 and later, you can use a ternary operator. The syntax is:
$result = $condition ? ‘TrueValue’: ‘FalseValue’
This is an efficient way for simple conditional assigning.
Yes, you can use the exit keyword to terminate a script instantly in this case. This might happen if a prerequisite check failed or something along those lines. In addition to that, you can also use ‘throw’ in conjunction with exception handling to throw an error and stop the execution.
In this context, short-circuiting is related to how PowerShell evaluates conditions and is used by operators such as -and and -or. For example, ‘-and’ would only evaluate the second condition if the first condition was $true, whereas ‘-or’ would not evaluate the second condition if the first condition was $false. In both situations, the short-circuiting behaviour saves time and avoids unnecessary evaluations.
Getting the hang of if, else, and elseif Statements in PowerShell are key to making your scripts work the way you want. These simple decision-making statements help your scripts adapt to different situations and respond to changes, making them more flexible and efficient.
You can use them for everything from basic checks to more complex tasks, like combining conditions with logical operators or handling errors. As you get comfortable with these statements, you’ll also learn some advanced tricks like short-circuiting, pattern matching, and using switch for tricky conditions.
So, if you’re writing simple scripts or building bigger PowerShell automation systems, knowing how to use these statements will make your work easier and more precise.
Comments