How To Use Switch Statements In PowerShell
Think you can do better? Show us!
Using the PowerShell switch statement is a much better alternative to using multiple elseif statements. In this video, we’ll see how the switch statement works and all of the options it has.
In this techsnip we're going to talk about the powershell switch statement. The PowerShell switch statement is similar to the powershell if statement where it's evaluating an expression based on a few different conditions. So the switch statement is different in that it allows you to provide an expression up front, you can see, there in line 2 the left Paren and right paren that provides that expression but it cuts down on a lot of those else if else if else if statement that you may run into when using an if statement so the Switch Teamw orks by you. You pass an expression to it in parentheses and then you have expression that one or more expression values underneath that and then finally you had that default value, which is the same thing as the else value. The else keyword in an if then else statement. So to show this in action. Let's go over a simple example first so in this example. I have a variable inside that variable season is going to be summer and then I have the expression to being evaluated in this switch statement is the variable value so in this case, it's summer. So and it's evaluated it's going to evaluate each of those conditions every single one of those conditions against summer and then I have summer and there again to show you how this is going to work in a minute fall. Winter and spring. So it's evaluating that the value of the season, variable a number of different times. And if it doesn't exist. That means if it doesn't match any of those conditions. Then it's going to match the default condition, which is that is just going to say unknown season, so let's see how this works. OK notice that it doesn't stop at one, so it matched the summer condition and it says the season is summer. But it matched the summer condition twice actually goes down through each of those individual conditions one at a time by default. It will just do summer summer fall. Winter spring even if it matches. One it’ll match every single one of those that's kind of the default behavior. However, normally you will have a break keyword in here. The break keyword essentially says once you match the original condition. Then just go ahead and break so I will go ahead and put the other summer condition in here just to make sure it doesn't actually match. Both of those, this time and I will go ahead and run this. Then now notice that it just has one of the season is summer. That's because it break. It broke the code execution. After the first condition was met I was very similar to a break and continue in looped if you're familiar with that. All right, so next step is assigning a variable the next one is you can actually assign a variable directly to this before, I was just outputing it to the console with the write host command in this case. I'm assigning a variable message messages equals whatever the output of is the switch statement. So I'll go ahead and run this. And then now when you see. What that message is the season is summer powershell is able to just directly take the output of that switch statement. Whatever condition is met and go directly to a variable. Next instances, it can actually process elements in array, which is pretty cool. So if you have an array in this case. I have 2 seasons summer and fall and if I pass that array to the switch statement. You can see that will actually process each of those elements in the array, so that's a pretty cool feature of the switch statement that it knows it can iterate over each of those in the background. And just pass each of those to that switch statement so that's a good way to evaluate different elements in an array. Next we can use the wild card statement, so using the wildcard parameter on the switch keyword. We can match varied values based on a wild card, so the asterisk in this case. I have a short season of just SUMM and then for each my conditions. I'm matching just the first few characters so SUMM FAL WIN just the first few characters of it and then anything after that. We'll run this notice that SUMM then correlates to summer because my wild card character matched SUMM anything after summer I could put SUMM Yadda Yadda Yadda. And then it was still match summer as well, so that's a good feature for the wild card. Next I can use regular expressions or regex. Same example here. I can just use a season and in this case. I'm using the regular expression with a carrot or the hat SUMM FAL WIN that just a regular expression to define if the expression starts with SUMM FAL WIN or SPR so in this case, it does start with SUMM so returns summer so that's a really good other way switch statement works with regular expressions. And then finally the switch statement also works with script blocks, so notice before up here for all of my conditions. I was defining strings for each of those so I have a string on 133, 136, 139 and 142, there each of those is a string and I've been using strings for everything I could use strings or integers, there, but I could also use a more advanced data type called script blocks and in this case. I can actually have each of those conditions evaluate another condition, so I'm evaluating dollar age on 153 first. And then at 154 and 157 I’m then evaluating that again with an expression inside of that script block in this case. I have an age of 38 I’m evaluating that I'm essentially passing that 38 integer to my dollar underscore less than pipeline variable dollar underscore greater than equal to pipeline variable and then it's going to tell me if my age is under 38. I'm young if it's over 38. I'm old I guess I'm. Old so when I run this is just going to say your old so on 154 and 157. Each of those script blocks is evaluating to whatever that's going to be. So it's a really cool way to evaluate to map the switch statement evaluate script blocks by evaluating each condition based on a script block so that was a primer to the switch statement in powershell thanks for watching.