Conditionals and Operators

Conditionals and Operators in PowerShell: PowerShell uses textual operators instead of standard mathematical symbols (e.g., instead of ).

PowerShell uses textual operators instead of standard mathematical symbols (e.g., -eq instead of ==). This prevents parsing conflicts with bash-like redirection symbols (>, <).

  • -eq (Equal to) / -ne (Not equal to)
  • -gt (Greater than) / -ge (Greater than or equal to)
  • -lt (Less than) / -le (Less than or equal to)
  • -match (Regex matching!)
  • -and / -or / -not / !
$status = "Running"
$uptime = 50

if ($status -eq "Running" -and $uptime -gt 10) {
    Write-Output "Service is healthy."
} elseif ($status -match "Error") {
    Write-Output "Service encountered a problem."
} else {
    Write-Output "Service starting..."
}

# Checking if item is in an array
if ("Server2" -in $servers) {
    Write-Output "Server found."
}