04 / MAKING DECISIONS · LESSON 13

Let your script decide

Use if/else to classify a value.

10 min · learn at your pace

Ask a yes-or-no question

PowerShell comparison operators include -eq (equal), -ne (not equal), -gt (greater), -ge (greater or equal), -lt, and -le.

$ageDays = 45
$ageDays -ge 30

A comparison returns a Boolean. An if statement uses that value to decide which block to run:

if ($ageDays -ge 30) {
    'Needs attention'
} else {
    'Healthy'
}

Use elseif when you need an additional condition. For several named alternatives, switch can be easier to read.

Combine conditions

-and, -or, and -not combine or negate conditions. Parentheses help make the intended grouping clear.

($ageDays -ge 30) -and ($ageDays -lt 60)

Remember that = assigns a value. Use -eq to compare values. For a boundary such as 30 days, decide deliberately whether the boundary itself counts.

Try an example.ps1
$ageDays = 45
$ageDays -ge 30
YOUR TURN

Put it into practice.

Set $ageDays to 45. Output Needs attention when it is at least 30; otherwise output Healthy.

See an explained solution
One way to solve it.ps1
$ageDays = 45
if ($ageDays -ge 30) {
    'Needs attention'
} else {
    'Healthy'
}

45 meets the >=30 rule, so only the first branch produces output.