PowerShell Switch Statement - Different take on branching

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome everyone today we're going to look at the powershell switch statement which is another way of using if you don't want to use many ifs then switch might be the way to go so let's have a look so welcome on the another video for me my name is coming protesting and if this is the first time you've seen me this is i'm pretty much making videos about powershell and being a scissor admin but that was mostly powershell stuff around here if you haven't went through if i wanted to actually see bitmobile if i have the other video which i will post the link in the description so you might be looking at the e first and then we can look at the switch because switch is really a great replacement when you have a bunch of if if statements in the one if else if us if else and if we don't want to do that then we can actually replace them with switch so what's the basic construct of switch is pretty much over there and what we do there in the switch we obviously put our statement then we actually the value we testing against and then in the curry buckets we do a combination of one of the conditions i want to meet and then actually what is supposed to happen when it does so you can imagine that this will be if you know that statement then if else do this if as that if else do that switch is just another way of achieving that however it gives us quite a cool few features so let's have a look so obviously with the very basic one just to show you how it works so this is switch and i'm looking for the value 2 and if value 2 actually is value number 2 and if that number is there in that case that we should print out green so let's run it and also this code is available on the github so if you'd like to download this and follow along please go to the link in the description and you can just you know play around or just watch me what i do well up to you so let's run it as we can see two should print out green and it prints out green pretty much but well usually we do conditions when we don't really know what's gonna be i mean we might have a different conditions what happens so obviously we can throw a variable there so obviously now i will assign the variable with value 1 and that should print out yellow and it does it's just to show you we can actually do variable what as we can do actually we can within there we see the script block because the current buckets the name of them is actually script block we can actually assign variables and then use them outside the statement so in that case i will assign you know a value free which is which will give us this and then i will use that result actually to print out to the screen and i will also use it with the in the parameters so let's just see how this works and exactly the result is red which is what we accept and value is free it evaluates as true in that case it's red and therefore you will use red in this one this is red and also foreground color is red cool and although this is you see quite a few so this is quite a few quite a lot of repetition like in every single statement i'm actually assigning same variable and what i can do actually instead i can assign the statement itself to the variable so other than having you know result repeated four times are just doing that result equals the switch and then whatever the result of that switch is you will get assigned to that variable so effectively we get the same the same way how it works just less code so we are well more productive so let's try and as we can see zero is blue and therefore it prints it out as blue okay now what in the case because there might be the case where actually i don't have a you know that actually whatever i get what i'm checking against doesn't meet these conditions then obviously there are two ways i can do nothing and therefore nothing going to happen because all of these values will evaluate as false and then that effect will be nothing we get will get executed oh instead i can use that default keyword so as you can see and i put it at the end it doesn't be at the end but that's usually the common part if you throw it at the end so pretty much if all of these values will evaluate as false then run this so we can actually kind of nothing happened i mean nothing matches i can still actually do something i can work on so i know we quite often see that if there is you know file and if that file exists or it has this content and do this otherwise you know do something else or i don't know it's just i'm just following ideas but let's see so obviously now i'm doing number eight which is not there so we should see that actually we get warning on the screen and then the result will be white and there we go we got warning and this is white because we just simply doesn't have any cases it doesn't matter any case that we have specified right so this is switch so far what else we can do we can obviously because i in here focus on integers but we can also check it against the text so and we can also i just we can do this two in one go we can actually evaluate an expression isn't it so obviously if i run get host it gives me some default the basic information actually where my powershell session is and what executes that but i'm really after the name yeah so where actually what is my host what's what's hosting my powershell session so obviously if i execute this statement like that it will only print out the value of visual studio host which is visual studio host and in this statement so what happens here first of all powershell will resolve this one so we actually will get this state expression and it will extract this value from it and then actually we can switch against it isn't it so i have we have uh three cases one is visual studio host and now we tell me i'm using vs code one is console host that is running console and then default is i don't know what is it and in that case p as i can actually print out whatever came out from this house so this is also another way how i can actually access the value of the statement or whatever is inside the switch in my code execution so this is now in the vs code let's try to run this and i'm using vs code this is correct so just for fun i will now take this to powershell session okay in console around this here paste it run and i'm using console because if we actually check it now console host so this is all correct next arrays yes we love ice isn't it so obviously we can have a bunch of eyes and what we can do we can actually go powershell and go and evaluate each of the statements so f activity will be like a throwing uh for each loop so switch typing will actually do it for us so if i use you have a bunch of job titles and if any of this job title is met then powershell will print out actually who we need so we're kind of assembling a team so let's try and as we can see we have a bunch of this and obviously developers twice because i specified developer twice but as you can see i didn't need to write any loops any for each for loops powershell did loop it through this array for me and evaluated each of these values against the switch pretty cool what if actually i need to compare some values there because obviously sometimes i need to yeah so i don't know the very simple one whether you are noddled or not so whether you are 18 or more or you are less than that isn't it so what happens here we assign the age so this is 25 and i'm checking now this h and i'm you see here again i'm using a dollar say underscoring other words ps item so so whatever is in the switch will be assigned to that variable so in that case we should get the response of this is although yeah and it's an old load now just speed with caution and caution just hide those because the format is slightly different like you see to this point we could just do a quotation marks and my value or actually even just value to be honest quotation marks are not necessary for string but i just thought it makes it much more cleaner and obvious what we're tracking and in here actually what we have in a in in the comparison in my condition is actually curly brackets yes because this is script block actually powershell needs to run this code and evaluate whether it's true if it's false and then you you will then again in this another script will actually execute it so if you look at it we have one two three script blocks just to run one statement which is yep i mean once you start using you you you will get used to it it's just this format is there because script block effectively will tell powershell to go and run this script block actually is and it's called anonymous function because it's a function that powershell creates you on fly but it doesn't have a name so just something or in other programming languages it's called lambda function this is actually what script locked is in powershell okay so we've done it and we learned a little bit more about script block cool what else okay now this is something to be to be to be actually worry about paying attention because obviously and what what happens here i specify a string and now in here i have pretty much same thing but just with different kinds of capitalization what do you think was gonna happen is are we going to hit first one and then stop actually is it going to execute all three of one well let's see as we can see it executed all three of them because powershell obviously by default is not case sensitive so we'll just go and doesn't care whether it's uppercase or lowercase and in that case all three statements are true because all three statements will be something we'll have that we have that word and because partial is not case sensitive you will actually go and execute all three of them in other words our shell will go and execute every single of the state i mean execute you will test this condition this our case i guess each each of these conditions and as long as this condition is true you go and run my code so now what can we do actually stop it in case we want to well one of the ways is to use break so i've put it here in the one line just to let's just see what's gonna happen as we can see it went he executed the first one they executed the second line and then it obviously does break so it's stop so break is paying partial you break out get out of here stop but stop at this point however we don't really need to use this in that in one line what and in the semiconductors you probably are not very used to using semicolon powershell if you have multiple commands in one line so for example when you're passing uh running power you i mean what you will see you will see very often when you executing powershell from command line from cmd and you pass bunch of commands or you know like a one big long one liner when you have application that doesn't support partial natively you kind of can do that for the cmd so and that's actually the way how we can link multiple commands and in one line if you don't want to do that we can actually just split it like that and then we can remove semicolons they are there they can be there they are absolutely optional and we can run it and we can see same behavior so this is up to you i just one sec of one line i didn't see it was necessary but if you want you can split this oh we have some parameters to switch so let's have a look on some pointers we can actually take the powershell that this is case sensitive so it should only actually take care when there's only one case and in that case when everything is lower case it will just execute this one because in that case this is case sensitive although probably you when you work with this kind of stuff you use bit more of white cards isn't it the asterisk is the most popular so this we can tell powershell that i'm gonna enable white cards yeah by using this switch saying white card action telling powershell enable white card for me so in that case i can use white cards in the statements in the con in the conditions down there so pretty much saying this is my name camille and pretty much if that name will show up anyway in the condition then you go and run this code let's try and exactly there's notification for me so that's white cut you can use white cards like you would with normal string operators comparisons and we can obviously enable regex for some reason i always keep regex to the left as the last one so it's pretty much we do the same what this code did but now it's regressing instead okay that will be my quick hash crash goes on switch statement pusher hope you've learned something today and there you have it a powershell switch statement and to be honest i use switch statements a lot the moment when there are more than you know if else statement if i can i will actually replace them with switch because in my view they make code much more readable because i just know which condition i'm looking for to execute other than going for logic of no this is true this is false false true sometimes it might get confusing if possible because sometimes you just can't do that and you need to go in the nested logic then sorry but if you can then why not use it so i hope you've enjoyed i hope you've learned something new today if there's any particular topic you're interested in powershell or maybe generally in in scissor minoria then let me know in the comments i will see if i can do anything about that and so far so good have a great day bye
Info
Channel: Kamil Pro
Views: 1,257
Rating: undefined out of 5
Keywords: powershell, coding, if, branching, switch, else, 2021, pwsh, condition, conditional, default, decision, true, false, lambda, script block
Id: EqJ0lBO1rM4
Channel Id: undefined
Length: 15min 4sec (904 seconds)
Published: Wed Aug 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.