Hi, Damian Garbus here and Today I want to show how breakfast can help you understand Powershell conditions. It’s very important to understand so check this out. I will use the variables, so if you don’t know what these are, please read My way to understand Powershell variable.

Finding the example of life

My examples must be understandable for everyone. So now I want to show each of you some examples of how to understand conditions in PowerShell. I was looking for an example from real life commonly known by most people. Everyone likes to eat a delicious breakfast. Yummy, a delicious fresh bread :). Check how bread and breakfast can help you to understand conditions in PowerShell.

What is for breakfast

Please imagine that you wake up early in the morning to make a delicious breakfast for yourself and your family. You must decide what you prepare; you have a choice between delicious sandwiches or pancakes. Maybe sandwiches would be better, but you don’t know if you have bread at home. If you don’t have it, you have to decide whether to go to a shop or make something else for breakfast. So, let’s write it in PowerShell conditions and show the first example.

$Check_Bread = "I have bread"

if ($Check_Bread -eq "I have bread"){
    Write-Host "I will prepare sandwiches"

}
else {
    Write-Host "I must go to shop if I want to make sandwiches"
}
powershell conditions

Powershell condition will help you decide

I hope the example above was understandable for you. Let’s go to the next one but in the same story. Now you must decide between sandwiches or pancakes. To prepare sandwiches you need bread whereas to prepare pancakes you need flour. If you find bread you can prepare some sandwiches. If you don’t have it you must check and see if you have flour to prepare pancakes. If you don’t have any of the products mentioned above, you must decide to make something else for breakfast or go shopping. So, let’s write this situation via PowerShell conditions.

$Check_Bread = "I don't have bread"
$Check_flour = "I have flour"

if ($Check_Bread -eq "I have bread") {
    Write-Host "I will prepare sandwiches"

}
elseif ($Check_flour -eq "I have flour") {
    Write-Host "I can prepare pancakes"
}
else {
    Write-Host "I don't have bread and flour - I must go to shop"
}
powershell conditions

Conclusion

In all programming and scripting languages, you must understand conditions. If you understand conditions, your scripts will be more flexible. You can read more about this here in future posts. I will return to this one to explain more advanced examples. If these examples above are useful for you, please share and subscribe to always be informed about new posts.

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 MailChimp ( more information )

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