liveBook · Manning

Conditional logic

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

An if construct conditionally executes a block when its Boolean condition is true.

Conditional logic lets a PowerShell script make decisions. An If construct evaluates an expression that must produce $True or $False, then runs its script block only when the condition is true. section 5.2

Build a decision from these parts:

  • Start with a mandatory If and a script block. section 5.2
  • Add zero or more ElseIf branches when different conditions require different actions. section 5.2
  • Remember that only the first true branch runs; later ElseIf expressions are not evaluated. section 5.2
  • Use an optional Else block for the case where no preceding condition is true. section 5.2
  • Prefer readable brace formatting, especially when blocks contain several commands; PowerShell has no End If statement. section 5.2

Choosing a connection protocol

A function can select DCOM when -Protocol is Dcom and use WSMan otherwise. This keeps the connection choice explicit while protecting the rest of the function from protocol-specific setup. section 10.1.2

Combining conditions

Use comparison and logical operators to express a complete rule, such as taking action when a process value is greater than 4 or less than 2. Put each comparison in parentheses for readability. section 5.2

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

Use this skeleton to map one condition, multiple alternatives, and a fallback action before filling in real commands. section 5.2

Apply the construct incrementally in reusable functions: start with one tested decision, then add more behavior. For example, a service-logon function can use If with ForEach to update one or more computers. section 10.1.2 section 10.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
Explains Boolean expressions, branch ordering, formatting, and practical function use, but stops short of language internals.
Examples
Uses date and process thresholds, protocol selection, Get-MachineInfo, and a Set-TMServiceLogon exercise.
What it assumes
Assumes PowerShell expressions and comparison operators, while defining how their Boolean results control execution.
What you can do after
Builds branching PowerShell functions that select actions, choose connection protocols, and handle fallback cases.
How it connects
Connects conditional logic with comparison operators, Boolean expressions, script blocks, ForEach, functions, and remoting protocols.

Also explained in

Compare how 21 books explain conditional logic →

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.