Control Flow

The Night I Processed 1,000 Servers

It was 11 PM when I got the call. A critical security patch needed deployment across 1,000 production servers before morning. Manual deployment: 5 minutes per server = 83 hours. I had 7 hours.

I wrote a PowerShell script with loops and conditional logic:

foreach ($server in $servers) {
    if (Test-Connection -ComputerName $server -Count 1 -Quiet) {
        if (Get-HotFix -Id KB5034441 -ComputerName $server -ErrorAction SilentlyContinue) {
            Write-Host "$server already patched"
        } else {
            Install-Patch -ComputerName $server
        }
    } else {
        Write-Warning "$server unreachable"
    }
}

All 1,000 servers: patched in 2 hours. Loops and conditionals aren't just programming concepts - they're force multipliers for operations work.

If Statements

The foundation of conditional logic.

Basic If

If-Else

If-ElseIf-Else

Comparison Operators

Examples

Logical Operators

Switch Statements

More elegant than multiple if-elseif chains.

Basic Switch

Switch with Multiple Conditions

Switch with Wildcard Matching

Switch with Regex

Switch on Files

ForEach-Object Loop (Pipeline)

Process items coming through the pipeline.

Basic Usage

Using -Begin, -Process, -End

ForEach-Object Aliases

Foreach Loop (Statement)

Process collections directly (not in pipeline).

Basic Foreach

Nested Foreach

Foreach vs ForEach-Object

For Loop

Counter-based iteration.

Basic For Loop

Array Iteration

Decrementing Loop

While and Do-While Loops

Condition-based iteration.

While Loop

Do-While Loop

Do-Until Loop

Break and Continue

Control loop flow.

Break

Exit the loop entirely:

Continue

Skip to next iteration:

Break vs Continue

Real-World Examples

Batch Processing with Error Handling

Retry Logic

Parallel Processing (Simplified)

Common Pitfalls

1. Modifying Collection While Iterating

Problem: Can't modify collection you're iterating Solution: Create new collection or use for loop with index

2. Forgetting Break in Switch

Problem: Switch continues to next case Solution: Use break or understand fall-through behavior

3. Infinite Loops

Problem: While loop condition never becomes false Solution: Ensure condition will eventually fail or use break

Key Takeaways

  • If/ElseIf/Else for conditional logic

  • Switch for multiple conditions (more elegant than if chains)

  • ForEach-Object for pipeline processing

  • foreach for collection iteration

  • for for counter-based loops

  • while/do-while for condition-based loops

  • break exits loop, continue skips iteration

  • Comparison operators are fundamental to conditions

What You've Learned

✅ If statements and conditional logic ✅ Switch statements with wildcards and regex ✅ ForEach-Object vs foreach loops ✅ for, while, and do-while loops ✅ break and continue for loop control ✅ Real-world batch processing patterns ✅ Retry logic implementation

Next Steps

Loops and conditions are powerful, but scripts fail. In Error Handling, you'll learn:

  • Try/Catch/Finally blocks

  • Error types and $Error variable

  • ErrorAction preferences

  • Building resilient scripts

  • Logging errors properly

Proper error handling separates toy scripts from production code.


Ready to handle errors like a pro? Continue to Error Handling

Last updated