POWERSHELL SWITCH VS IF: This Is What Professionals Do

Intro

How many times are you using IF-ELSE statements with more than 4 ELSEIF conditions? I want to show you comparison SWITCH vs IF statements in Powershell. Why should you consider to use the SWITCH statement? Check this out.

Firstly, let’s check, what is Powershell SWITCH Statement.

What is Powershell Switch statements

Sometimes you have to process many single conditions. I’m sure that most of Powershell beginners users will choose IF-ELSEIF-ELSE statements. Here’s an alternative way to do this with SWITCH statements. Let’s check an example – determining vehicle by wheels count.

$WheelsCount = 4
if ($WheelsCount -eq 4) {Write-Host "it's a car"}`
elseif ($WheelsCount -eq 2) {Write-Host "it's a bike"}`
elseif ($WheelsCount -eq 0) {Write-Host "it's a boat"}`
else {Write-Host "Unknow"}
if-else statements
$WheelsCount = 4
switch ($WheelsCount) {
    4 {Write-Host "it's a car"}`
    2 {Write-Host "it's a bike"}`
    0 {Write-Host "it's a boat"}`
    default {Write-Host "Unknow"}

}
switch statements

Powershell SWITCH vs IF

Maybe you are wondering why you should to use SWITCH statements. What’s the difference between Powershell SWITCH vs IF statements. You can always use IF-ELSE conditions. However, I recommend you to use SWITCH statements of a few reasons.

Firstly, you should decide to use SWITCH statements because in some scenarios it’s faster.

I will use the same example that I used before to test these statements’ speed.

Powershell switch vs if

As you can see in this example, IF-ELSE statements are faster. Let’s check what happened if I add more conditions to these examples and change variable to string type.

Powershell switch vs if
Powershell switch vs if

As a result, the SWITCH statement is faster for more conditions but the result is similar.

If you have more than 4 conditions in the Powershell script, I recommend you to consider the use of SWITCH statements.

Another reason to consider

Another reason to consider to use the SWITCH statements is that the Powershell code is easier to understand. It helps to modify code faster and reduce the number of errors.

However, you have to remember that you can’t replace all conditions by SWITCH statements. It only helps you to tests expressions with a single value. For example, you cannot use it to check if the value is greater. To do this, you have to use the IF-ELSE statement.

Conclusion

I hope that I explain why you should consider using SWITCH statements in your Powershell scripts.

To read more about Switch statements I recommend you visit Microsoft documentation page or great article on powershellexplained.com.

If you have any questions about this, please leave comments.

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.