liveBook · Manning

Elif clause

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

An ElseIf clause is a conditional branch in an If construct that has its own expression and script block and is considered when earlier conditions are false.

An ElseIf clause adds a conditional branch to an If construct. It has its own expression and script block, which runs only when its expression is true and earlier conditions were false. section 5.2

If (<expression>) {
    # code
} ElseIf (<expression>) {
    # code
} ElseIf (<expression>) {
    # code
} Else {
    # code
}

Use multiple ElseIf clauses to test conditions in sequence. PowerShell stops at the first true expression, runs that script block, and skips the remaining branches. section 5.2

Choose one action

An ElseIf chain can separate high, low, and default cases: If ($proc.vm -gt 4) { take some action } ElseIf ($proc.vm -lt 2) { take some other action } Else { do nothing }. Only the first true branch executes. section 5.2

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Defines syntax and first-match evaluation, but stops short of detailed implementation internals.
Examples
Uses process virtual-memory thresholds to demonstrate high, low, and fallback branches.
What it assumes
Assumes PowerShell expressions and comparison operators; defines that expressions must produce True or False.
What you can do after
Apply it via PowerShell to run exactly one branch from an ordered set of conditions.
How it connects
Connects ElseIf clauses to If constructs, expressions, script blocks, comparison operators, and Else clauses.

Also explained in

Compare how 3 books explain elif clause →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.