Control Flow
The Night I Processed 1,000 Servers
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"
}
}If Statements
Basic If
If-Else
If-ElseIf-Else
Comparison Operators
Examples
Logical Operators
Switch Statements
Basic Switch
Switch with Multiple Conditions
Switch with Wildcard Matching
Switch with Regex
Switch on Files
ForEach-Object Loop (Pipeline)
Basic Usage
Using -Begin, -Process, -End
ForEach-Object Aliases
Foreach Loop (Statement)
Basic Foreach
Nested Foreach
Foreach vs ForEach-Object
For Loop
Basic For Loop
Array Iteration
Decrementing Loop
While and Do-While Loops
While Loop
Do-While Loop
Do-Until Loop
Break and Continue
Break
Continue
Break vs Continue
Real-World Examples
Batch Processing with Error Handling
Retry Logic
Parallel Processing (Simplified)
Common Pitfalls
1. Modifying Collection While Iterating
2. Forgetting Break in Switch
3. Infinite Loops
Key Takeaways
What You've Learned
Next Steps
Last updated