How changing tires can help you understand Powershell Foreach Loop

Hi, Damian Garbus here and this is Poshland blog about PowerShell. Several posts ago I wrote about the best reason to learn PowerShell. Today I want to show you fast tips to understanding PowerShell “Foreach” loop. It is a basic of programming knowledge, and I think that PowerShell is the best way to understand it. Let’s do this.

What is loop in programming?

Powershell “Foreach” loop is a sequence of tasks that is continually repeated until a certain condition is reached. There are many repetitive tasks in our lives. For example when you want to change tires in your car you have to unscrew the wheel, change the tire and fix the wheel. This is just for one wheel. But a car has four of them. So you have to repeat the same tasks for the remaining 3. Let’s check how you can write in PowerShell.

Why is Powershell “Foreach” better

When you want to write step by step for changing tires it will look like this.

Write-Host "Unscrew Front Left Wheel"
Write-Host "Change tire"
Write-Host "Fix Front Left Wheel"
Write-Host "Unscrew Front Right Wheel"
Write-Host "Change tire"
Write-Host "Fix Front Right Wheel"
Write-Host "Unscrew Rear Left Wheel"
Write-Host "Change tire"
Write-Host "Fix Rear Left Wheel"
Write-Host "Unscrew Rear Right Wheel"
Write-Host "Change tire"
Write-Host "Fix Rear Right Wheel"
powershell foreach

When you use “Foreach” loop it will be shorter. You can assign a variable for your all wheels and use it in a loop. If you don’t know what variable is – check my post How to understand Powershell Variable.

$MyWheels = "Front Left Wheel", "Front Right Wheel", "Rear Left Wheel","Rear Right Wheel"
foreach ($wheel in $MyWheels) { 
    Write-Host "Unscrew $wheel"
    Write-Host "Change tire"
    Write-Host "Fix $wheel"
}
powershell foreach

What when you have two cars

When you have two cars we can use another variable to define all of your cars and use a loop within loop.

But first check how does it looks like without “Foreach” loop.

Write-Host "Unscrew Lamborghini Front Left Wheel"
Write-Host "Change tire"
Write-Host "Fix Lamborghini Front Left Wheel"
Write-Host "Unscrew Lamborghini Front Right Wheel"
Write-Host "Change tire"
Write-Host "Fix Lamborghini Front Right Wheel"
Write-Host "Unscrew Lamborghini Rear Left Wheel"
Write-Host "Change tire"
Write-Host "Fix Lamborghini Rear Left Wheel"
Write-Host "Unscrew Lamborghini Rear Right Wheel"
Write-Host "Change tire"
Write-Host "Fix Lamborghini Rear Right Wheel"

Write-Host "Unscrew Porshe Front Left Wheel"
Write-Host "Change tire"
Write-Host "Fix Porshe Front Left Wheel"
Write-Host "Unscrew Porshe Front Right Wheel"
Write-Host "Change tire"
Write-Host "Fix Porshe Front Right Wheel"
Write-Host "Unscrew Porshe Rear Left Wheel"
Write-Host "Change tire"
Write-Host "Fix Porshe Rear Left Wheel"
Write-Host "Unscrew Porshe Rear Right Wheel"
Write-Host "Change tire"
Write-Host "Fix Porshe Rear Right Wheel"
powershell foreach

And now with “Foreach” loop.

$MyCars = "Lamborghini","Porshe"
$MyWheels = "Front Left Wheel", "Front Right Wheel", "Rear Left Wheel","Rear Right Wheel"
foreach ($car in $MyCars) {
    foreach ($wheel in $MyWheels) { 
        Write-Host "Unscreew $car $wheel"
        Write-Host "Change tire"
        Write-Host "Fix $car $wheel"
    }
}
powershell foreach

Conclusion

I hope this post helps you to understand PoweShellForeach” loop. Loop is the basic element of programming so you also will find it also in other languages. Maybe you know another way to understand loop? Please write in the comments below and share if you like it.

Get Access to Poshland Resources for FREE!!!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailerLite ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.