An expression is a PowerShell statement that evaluates to a result. In an If construct, that result must be $True or $False; comparison operators such as -eq, -ne, -gt, and -like help produce it. section 5.2
Use expressions to control which automation path runs:
- If evaluates its expression and runs the first matching script block; optional ElseIf and Else sections handle additional outcomes. section 5.2
- Switch evaluates an expression against conditions and can run every matching block, or a default block when nothing matches. section 5.4
- Combine comparisons with logical operators, grouping each comparison when clarity or precedence matters. section 5.2
Test the expression first
Start with one expression in the interactive PowerShell console, confirm its syntax and result, then build the larger script around it. For example, test whether a value is greater than 4 before placing that condition in an If block. section 9.0 section 5.2
If (<expression>) {
# code
} ElseIf (<expression>) {
# code
} ElseIf (<expression>) {
# code
} Else {
# code
}
Use this compact shape to place an expression inside If, ElseIf, and Else decision branches. section 5.2










