0
0
PowerShellscripting~5 mins

Why operators perform comparisons and logic in PowerShell

Choose your learning style9 modes available
Introduction
Operators help us check if things are equal, bigger, or smaller, and make decisions based on true or false answers.
Checking if a file exists before opening it.
Deciding what message to show based on user input.
Comparing numbers to find the highest score.
Testing if a password matches the stored one.
Running different commands depending on system status.
Syntax
PowerShell
if (<condition>) {
    # code to run if condition is true
} else {
    # code to run if condition is false
}
Conditions use comparison operators like -eq (equals), -ne (not equals), -gt (greater than), -lt (less than).
Logical operators like -and, -or combine multiple conditions.
Examples
Checks if variable a equals 5.
PowerShell
if ($a -eq 5) {
    Write-Output "a is 5"
}
Checks if age is between 18 and 64 inclusive.
PowerShell
if ($age -ge 18 -and $age -lt 65) {
    Write-Output "Adult"
}
Checks if name is not 'Admin'.
PowerShell
if ($name -ne "Admin") {
    Write-Output "User is not Admin"
}
Sample Program
This script checks the score and prints the grade based on the value.
PowerShell
$score = 85
if ($score -ge 90) {
    Write-Output "Grade: A"
} elseif ($score -ge 80) {
    Write-Output "Grade: B"
} else {
    Write-Output "Grade: C or below"
}
OutputSuccess
Important Notes
PowerShell uses -eq for equals, not == like some other languages.
Use parentheses to group conditions clearly.
Logical operators -and and -or help combine multiple checks.
Summary
Operators let scripts compare values and make choices.
Comparison operators check equality and size.
Logical operators combine multiple conditions.