PowerShell For loop

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome everyone today we're going to talk about powershell and the fold loop so if you ever wonder how to use that loop well this is the video for you mine is competition and let's have a go [Music] okay the for loop why would i bother why why would i need loops well loops is you know if you need to repeat one one action more than once or even zero two more times depending you don't always need to use loop however for loops is pretty much basic i think like everywhere and knowing it is really helpful and especially when the initial syntax might look bit odd once you learn how to use it you will be well you will kind of need to use it at the very end of this video i have a i have an actual request scenario where i was encountered recently with the application insights from azure where it gave me a set of columns and rows and i needed to glue them together and fold up apparently came to help so that should be great if you're interested because i have all this code on the github and you can find the link in the description so if you would like to actually have a look on the code yourself then please go download it use it enjoy so what is the basic syntax for loop it's pretty much the 4 with 4 which is our our statement and then we have pretty much something called initial value which is set where we start counting from what value what is it what is the variable then we have certain condition that will happen and whether that condition evaluates true or false the loop will run or you will exit and then what happens when we get to the end of my code so i've done well my code i did it and then what to do with after that was the condition and that's usually when we incremented by one usually we don't have to and then we pretty much for every loop you will run my code so to make it bit easier i made a little little diagram which i hope will help you to get the idea so you see that's where we start and now this is our initialize part so initialize index and that's like 99 percent of time you see something like dollar sign i equals zero and in that case stands for either initializer or index and y0 because i guess we always zero index in in the in the arrays and etc so probably that's where it came from but this is why the last and i or you know the language variable is called i okay and then we have our condition so that's our if kind of if statement although we don't say so in our condition is for example that in that case my eye is less than 10 as long as it is less than 10 it will my loop will go so obviously we stating at the beginning that this is zero so in that case here it is less than 10 so let's go so when it's true you will run my code you do whatever i tell you to do and then at the end we increase counter so now we got here and our loop is one still less than 10 so we run again to the point we get that this is 10 and what is actually 10 then you will exit the loop so this is how it looks on the you know my diagram kind of visual point of view but okay let's dig in let's have a look at the of the actual code so i made the very first example very explicit just to give you a bit more idea so we see we starting with the as we can see this is our initializer so initial value then we have this separate i didn't mention yeah we separate statements with the semicolons so yeah i mean i put them here i didn't say them loud but you can see so this is my initial value and in that case i just created my variable just for fun to don't be you know dollar sign i like it like you always see it you will see it many times enough in your life that yeah we don't need to repeat it here then that's this is my condition so what i'm checking against obvious and obviously in that case i'm checking that this is less than 10 and then my variable again i made it very explicit because we could do my variable plus plus but i just want to see plus one and then the loop we run and then what we're going to do we're going to display pretty much the value of my variable is this so whatever device and i made it just so it doesn't like you know goes through the screen in a matter of less than a second so i made it by we wait two seconds after each iteration so you can just kind of see how it goes at least for the beginning okay so let's run this bit so we can see my variable is zero it's one two so we keep iterating until we have this number and then the loop will stop but let's give it a second as you can see this is very exciting looking on the timer like yeah i can't do it for hours good so now is nine so what's happened why did not turn because then we increased at this point we were at nine yes at this point we're at nine the sleep ended so it increased the value by one so at this point when this check happened it was already ten that's why i didn't run 10 my valuable is 10. okay obviously we can do it this is a bit more this is a bit more familiar because we have now familiar more popular because we have plus plus yes so rather than do x plus c plus one plus plus equals plus one so now if i run this and we going this up to 99 so let's go see a second and then yeah we have 99 iterations because 98 is is a same story like with 10 here okay and we can also you know do something different like for example we can if you want to if we stay high because maybe i like to go decrease or maybe actually the value are working for i'm going with the lease i'm going down you know down here other than up here then absolutely i can do all the then i can do all the lists like you know go in decreasing so obviously now we broke out six why because we set our temperatures as long as it is greater than five yeah i don't need to be limited to whatever icon so i think that gives you the basic idea how the fall loop works so let's now do you know have a look at some bit more power surely stuff or what we can do to be more creative with it for example here you see at this point i don't have a variable i don't specify it and this is completely legal i can obviously do that and the reason for that is is that i specify my variable outside so i call that outside just for fun so we have we're going from seven until 15 and then we iterate by one by one so let's try and we see started from seven and then on 14 as expected okay great but what about you know you will quite often get uh an array of things so it's not like you will get numbers you you might actually need to you know go and iterate the things that you get already maybe usually some software will give you that yeah isn't it i need to do something with that data so let's specify our i and let's double check count yep that's four items we have four items in in the in this array and ice in the case has to show you can actually request a specific you know specific item from that you don't need to do so and you don't need to go release all of them you can ask for the one specific so in that case you have for example pets and now in square brackets i put two that will be in fact affect a fight item why because again eyes are zero indexed that's why we get actually a third item okay so for example i like to you know list my pets who doesn't like to display pets talk about pets so let's do it and we can see it did we run this once and now we under in the eye so we see this now probably start to boiling to you because we have the last sign iron yeah and now look what's going on here i'm telling you until my our index is less than count so that was four as we know from this point and then keep looping and what's going on here you have pets and i have the left side iron so other voids when this loop runs for the first one that will be pets square brackets 0 then you run again square brackets 1 2 and 3. so you will pretty much iterate for that eye for me but whatever i don't know maybe i like to do a bit more bit more whatever i want to actually put a number at least because i can isn't it so you know sometimes we like to put some fancy looking stuff in the shell and we can absolutely do that so obviously what we're going down here it's blank line then again my pass in order but the what happens here you see i have obviously my standard index i have now i'm getting the length of my of my area which is that count is actually our length also that's you can use that both words intermediary and then we take by one but what i've done here because obviously we as the human beings we can't from one we are one if you would say in the programming languages programming context we are one index yeah we need not zero base index we have one base index so obviously i will want to start to think from one so what we've done here i just created another variable which is you know number is whatever i is and then add one to it so in that case when i am on the let's say phase example which is cut yeah so when i'm grabbing the cut it will take my number so the i is zero plus one it's one so that's why we have one and it gets me one from the i so we can make it bit more human readable great what else we can do we can works with strings if we want to so for example here i'm you know beginning that the text is like literally an empty string strength and then until my text length because pretty much when you have a text it has certain you know name of characters it is length so until it's 10 characters long you will keep adding a symbol add to it so what's what do you think what's gonna happen here what's how we gonna what's gonna end how we gonna get it well let's have a look we get some sort of i will call it half of the christmas tree if you if you wish but you get the idea so obviously we started with blank and i have one two and etc up to the is it nine yeah nine nine arts in the at the uh in the string so i can obviously use it also to work with strings all right but what if i need to sometimes you need to you know create a a loop within a loop so we need to like iterate for the couple of different things so let's have a look so what is this this is a shortcut for creating another so effectively what i'm doing here i'm telling you create me an eye which start at size at 1 and that 10. so that's what it does and if we look for it pretty much what we got here we got we're iterating through i so the for and i will checking against the rows and then we're taking for the j where we turn against columns of it so and then we do some more things so i wonder if you can tell what this will do because obviously this is a bit more complex i wonder if you guessed it's something what i had to do a lot of when i was back in school and this is pretty much to do a multiplication so let's have a look from the beginning what's going on here that will probably it will probably be easier so where we have it so as we can see it increases that way you see so it gets to the 1 through 10 then in increase this one by 1 and then again gets to 10 and that's some mass for us so what happens here well for for the starter to make it very visible i made it that grab me the roll number i so in that case there will be face number which is zero i'm sorry as a zero number first number from the eye so that would be one and then give me a column which is j so that in that case there'll be also one and then we multiply them so what happens here pretty much we have rho by i mean the value from 0 times value from the columns and this is result and that's what we did play display here as the t i mean i probably meant text when i did that and so first one obviously happens we have both we know will be once then this loop happens yeah because obviously we went for one so it happens then j is increased to two so that's all we have two and then three for ten now probably this is the most interesting in part for you what happens here because obviously that we get to the point where this column is you know we get to the reach we are 10 so j is kind of finished done yeah jainist already this evaluates us false so it will not happen anymore because j will be will be more than that so what happens now so now this loop is finished we come back to this loop to the i to the first loop which goes for our rows and that tool gets in increased by one so now one became two and again now because that will be again iterates 10 times for the j row and you can see that this that's why you have a bunch of ones at the beginning and then this increase then when we get to the 10 our phase value increase to 2 and then now again this loop starts from one to ten and that's how you can do like a multiplication so yeah that's something so you know you can do nested loops if you need to and that's how they work so first off we need to be false before we can even come back to the top four we could do even more of them but probably that will give you the idea what else we can do well what if i don't specify the condition yeah so in that case i specify my index specify what happens then and then loop number so what your record is going to happen now you will keep running until i guess memory will get clogged up and then it will flash look have a look what's going with my cpu at the mind it just yeah uses it all the time and look it even writes one megabytes per second to my hard disk this is crazy isn't it but yeah and the only way to get out of this loop is to literally press ctrl c to break it because i didn't space if i condition i could put the condition is true and we'll just keep looping looping looping endlessly so we cannot always specify the condition we don't always know what it might be like we sometimes are is like we don't know we cannot foresee it and there is no way to know it so what we can do we can for example inside the loop we can specify condition when want to break out of the loop yeah so as you can see it's same case from the before when we don't have condition however here i have if this is equal to 10 then we're going to break so break is a special word in brushes spring that literally will tell you to stop what you're doing right now just you know hold your horses and stop so when we run this boom then breaking out you see so we got to that sorry no 10 1000 we display this breaking out statement and then it broke yeah so let's run it once again just make sure i wasn't lucky well i wasn't that's how it works so that's how you can you know if you meet certain conditions just get out of get out of here and move on great and now this is my my this is my case what i had to do with azure application insights now i don't believe that every one of you will have another application insights at hand so i kind of recreated a scenario where we can literally just run it locally and what applications i was you know running quieter for to achieve some information and pretty much what it did it gave me an object that had something like that like a bunch of headers and a bunch of reals and as you can see within headers we have obviously one like a flat array that is just literally headers and we have a bunch of roles but in rows i had like a you know a multiple like a roles like a multiple items but they didn't have headers and obviously while working with powershell i always want to return you a powershell object like this is natural to powershell and i have par when i have partial object i can you know request it i can do something about it i don't need to do any sort of conversions so we see the idea is i always want to have a partial object whenever i'm outputting something i'm giving you you know write you some code or something i want you to have a powershell object so obviously we need to somehow glue to through glue these properties together somehow because i cannot just have blank you know just the names because that would be a bit messy i won't be partial object so how we do that well we want run so you see now if i expand this we can see this is my array if i do rows and rows now what's where i'm going to i literally commented every line one by one here so just we go for this slowly so what we need we need to store information somewhere yeah because when i create one object i need to store it somewhere i want to create another one and another one another one to store them somewhere and the perfect case for it to use well it's not very but it's actually to use something would probably look very familiar to people who seen c sharp or not framework to actually create something called list because problem is with array by definition arrays cannot be expanded so if i go exactly i that have three properties or three objects or whatever if i want to expand it they'll be very costly they'll be a performance hit and they are not redesigned for that so i cannot use that here because i don't know i mean in that case i will know but what if i you know will add more up more properties more also if i add more headers i don't know it isn't it so in that case list update pattern gives you a flexibility like ice although you can i mean flexibility you can use them as eyes although you can expand them on fly so obviously in that case i'm just declining a empty list which is we can see system collections generic list and now here specific actually what is expect i'm just expecting object and object can be anything really okay so now we have a we have we have a loop isn't it so obviously and here we probably use or if not you've maybe had about four each which is another kind of loop and i made it to make it very obvious what's going on with the code in her inside here and if you haven't had about four inch well that's how you use it in the foliage we pretty much telling it to create like a local variable on fly firmware so for each row in rows which i think so for every single row yeah in this rows i want you to do something with every single row okay so we will literally working like with one row at the time okay then i need to create a blank powershell object so that's i need to i will be iterating for the information i need to add something to it i need to keep adding properties to it so that would be you know my header and my and my value header name header name so that's i'm declaring an empty partial object and how we do that we declare a hash table and the type is ps custom object and this is whenever we are putting something to the shell this is pretty much the most standard way of doing that and how we can do it on fly and now actually we're going to use you know our row so now what we're going through because obviously we're going for the roll so we're on the one so in that case this is these values so it's camille the email and the number so obviously we're going to start from the property zero as many as headers there i have a look how many you know how long the headers count is so it's free so we're going to iterate three times and then we again iterate one so what's going on here i have an object so i can use this one which is that member which will literally add be the property to this one so this one allows me to you know what the timeout and what's going on here i mean yeah this allows me to add properties to the object so it won't be blank after earning this you will populate it with this values so what we're doing remember type is node property which means is a field pretty much and then the name is headers and then in that case we under obviously first one zero so zero will be first name and then value is my row which is zero yeah row i zero so that case will be cameo and then it will iterate again and go to one so in that case my header will be one is email and value is email and then the same for the phone number and once we went for once we went for that you will now add that record to my list that we've created here so that way you will pop we start populating the result and once we got to the end you will again come back to 4-h and now you will take the second record from the rows so that would be this it will take this whole lot and again so now row yeah you will on fly become this record and then again we again create a empty record because we need to clear it and then we're going through again filling header with this with the first value email the second value phone number of the first value then we again for each will again grab the next record replace it with the that value we replace now with this load and again we match these things and after all all of that you will this we can display the whole thing so let's try it so i think i've already did the header so let's try and there we go we have a real proper powershell object first name email phone number it's all glued together so obviously now if i go to result yep i can do that but i can do for example email or i can grab a face name done so obviously we had a this was yep that was done this way in that way with the help of the well in that case for each and the for loop i was actually able to glue this together so i hope that's pretty much all i have prepared for you hope you've enjoyed that and you learned something new our fault look that image in essence with a little bit of for each bit more advanced topic but hopefully you will find it useful at some point or maybe you know at this point you will go and start learning what how to use for each why not because that's for ages to be honest the most popular one i would say personally that's the one i've been using the most but sometimes you literally have to use four so i hope when you get to this obscure part you'll find this video useful hope you have enjoyed have a great evening and i'll see you next time bye bye
Info
Channel: Kamil Pro
Views: 632
Rating: undefined out of 5
Keywords: powershell, loop, statement, for, foreach, while, do, repeat, pwsh, azure, insights, application, coding, programming
Id: YQnBVn-9SN0
Channel Id: undefined
Length: 24min 14sec (1454 seconds)
Published: Wed Jul 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.