How to use PowerShell foreach

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and today's show we're gonna talk about a PowerShell statement for each not to be confused with the command light for each object we will look at how 4-h is used what you can do with it and then maybe in explain a little bit about the difference between 4h and for each object should be fast should be fun but first here's our intro man that music gets me pumped up hi my name is Shane Young with bold zebras those guys and today's show is about another one is powershell core piece of the functionality you need to know how to use and as a for each statement it's often confused with for each object but it's actually a different command there is a separate video on for each object if you want to see that one but with for each the statement we're gonna look at how it works which you can use it for how to loop with it and all the fun things you need to know about it to make you better at PowerShell so it's really pretty quick and fast video so let's just switch over to my desktop and check it out over here on my desktop we're gonna open up the ISE and so you can see I've got it got all the commandlets we're gonna run through here and the first thing I'm going to do is we're going to import a CSV file right and named it file dot CSV because I'm really creative so if we switch over there real quick I've super zoomed in excel so you can see it but this simple CSV file has got three columns right object property name object property color and object property number I did that to make it easy to address and figure out what we're using and then I just put in values name zero name one name two red yellow green you can read so I want me to anymore but that's our simple CSV file that we're going to import also keep in mind it the down the description below is a link where you can download the CSV file and this ps1 file I'm using so if you want to go through this on your own you want to follow along I make those available to you okay so we're going to import that file so we'll hit that line and you can see that's where it'll pull in and so the thing I'm going to do is I'm actually going to do this line here this second line and what we want to do is we want to put this into an array variable service it run that selection so then now if we type in our objects array you can see we have the same information if you're not familiar the rays check down below there is a video on working with arrays available also very handy way to iterate through objects especially in this case because what we're going to see here with for each is it when it does it takes a collection in this case our array and it's going to run through and iterate through that loop one time for each one of the objects in that array that's one of the great uses of it so you can either take something like get process and dump that into a variable and then iterate through that or in this case you know I did put it in a CSV file so whatever you're working with it doesn't matter but so let's say look at our friend for each alright so for each you notice it's written this way and the key with for each is that it'll always be at the beginning of the line you're not typing anything into forage it doesn't accept any of that type of stuff 4-h is kind of a standalone statement it starts a line off so for each and so then we have these nice little braces or parentheses and you can see it's for each object in our objects array okay so what does that mean this is why the confusing parts of using this command up for me especially in the beginning so we know what's in our objects array right it's a array variable that we created it could had any name but that was name I gave it and in that array right there's multiple objects all right we can confirm that down here on the command line I'm just saying objects array dot count so there's three things in there just as we expected so we're saying alright for all of the things inside of there assign them the variable name object this is what's confusing because this can be anything right you can this would be object this could be dog this we can't squeeze change your hero whatever you want to be this is just saying every for every object in there we want to do the loop one time and what we're going to do is we're going to assign the current object the value of object alright once you guys could've been called anything here go ahead we'll change this what changes the dog LR signed dog and what make sure we change it here dog all right it doesn't matter so for each dog and our objects array that's just each object in there what we're gonna do so we're gonna do whatever is between these two curly braces here and so first things I'm just gonna do a right - host the name of the object is dog object property name so let's run this line and see if we get actually before we run it let's clear screen click on that let's highlight right here boom run that line all right so it ran three times right one two three and the first time through the name of the object is name zero the second time was name one and name to which matches up purpose perfectly with what was in our variable right name zero name one name - because that's all this did so everything this curly braces was ran three times because that was a number of objects in our array and so the first time through it was this first line right so the names you read in 100 the second time through it was named one yellow and a thousand so this let us have a variable that was going to change every time the the loop ran without us having to worry about any mechanics or figuring out which which object in our array or our collection when we're messing with so that's pretty handy so let's just say this step further it's the same thing for each this time we do object in our objects right right because this name doesn't matter just needs to be a name that's unique we're gonna have here right - host and then we're gonna say the name of the object is object property name - foreground color object object property color alright so if we do if it did this correctly we should see our name 0 name 1 a name - and the font color should be a different color each time seems pretty reasonable to me so let's go over here make sure that line is highlighted and then when I throw it go and there you go the name of the object is named 0 and read the name one was yellow and name two is green which matches up exactly the up here right so showing you can call multiple properties of that object the same way you would any other one it's also you know worth noting if saying my object in this example I had methods right we could call the methods the same way so you know maybe I'm iterating through a whole bunch of mailboxes and I need to you know run a update on all of them or something like that right I have that capability because the the whole object is there in the curly braces it's just a matter of what does it work for us all right so let's make this a little more complicated so real quick I'm going to create a variable called total number column equals zero just defining my variable and setting its value to zero so power show doesn't you confused it knows it's a number and we're going to now look at all right for each object in our objects array we're gonna do we're gonna write host the name of the object is object property name foreground color is our color right and then we get down here and we have a semicolon and semicolons aren't unique to for each but what semicolons do is they're just saying hey that line this power show you just did that hole right host statement it's done so stop processing that start a new line and go do something else disconnected to that so this is how we can run multiple things inside of our curly braces so here what you'll see is then I hate all right total of number column right so we know we're gonna assign that of value and it equals the current total which is zero plus the object and it's property number whatever that might be and if we were to look at our object again or our array again right object property number is a hundred a thousand and ten thousand so should be eleven thousand one hundred when it's all said and done right if my quick math is in a good place so let's take and run and this line all right so name one zero wanted to came out the way we wanted and now we'll run this line so just right out of the screen where is the value of total number column eleven thousand one hundred woohoo so we just showed how to do a multi-line for each statement in there and once again it's still just working with the properties so let's set it back to zero now one of the annoying things here right especially for me where I've got a super zoomed in to make it easier for you guys to read is that all this vertical or vertical horizontal scrolling that I'm currently doing so one of the things that I want you to note is that with for each object or for each you are completely capable of breaking that up so I see for each objects in our objects a here's my open curly here's my close curly and so then now what I'm doing is I'm just running all the different things I want to do in there one line at a time just like I would a normal powershell script it's just inside that block it's gonna be looped through once for each object in our case three times you also knows out here that I don't have a semicolon you could definitely put it out there or not it doesn't matter PowerShell is smart enough to know that that line is over you started a new line so it's going to start a new thing but some people like to just put semicolons and in Duvall these lines to make sure they avoid confusion once again it doesn't really matter you also see here right so we did the right host just like we've been doing we did the total number column like we just did so same two steps there and then I added an if statement inside there right because once again inside these curly braces it's just a block of PowerShell it's gonna be ran multiple times for your loop so you can put other loops inside there right we can get nested loops inside of loops happens all the time just finished a migration project right where my for each statement had I think 200 lines inside its curly braces and there was like seven if statements in there and two other for each statements inside there it was just chaos and pandemonium not something you guys want do today but just know that that's what a lot of these mega scripts start to look like when you start writing scripts you know iterate through hundreds thousands or tens of thousands millions of items whatever you're doing it does happen alright so what is my if statement two well one is I need to make a video on the if statement that's that's not today but sometimes let's say if object object property number right so the current property number is greater than a thousand right out of those this object is bigger than a thousand and the name of that object yeah pretty generic pretty easy but you know I'm lazy what can I say so let's just take all of this and we'll highlight it and we'll say we run our selection actually square screen first clear our screen we'll run our selection so here you can see the name the object is zero one and two right and the colors as we expected this object is bigger than a thousand so name two objects is bigger than a thousand that's correct and then if we run slime we can see that total number calm ended up being 11100 again so everything worked exactly the way we wanted but in this handy-dandy little easier to read method right so 100% of the time my right my power show like this you can see I did the tabbing all that fun stuff it just makes it easier for you to follow along and not have to scroll back and forth all the time so alright so that covers for each now what it's been a second real quick and just talked about for each versus for each object so for each the statement as I as I called this one I think it's this correct name this one is you know you take for each you have a variable with all your objects in there and then you iterate through them right well how do you just did for each object you're going to pass it something on the pipe right so it's gonna be something like yet process and then you pass it over for each object and then you know for each one of those objects blah blah blah we do something like that so that is going to be the difference is in this example I had everything are in a variable in this example I took a command lit and this took advantage of the pipeline output of that and I used it for the most part for each and for each objects are mostly interchangeable if you start to get into you know some super advanced stuff you'll find out that there are some performance differences between the two we're not going to talk about it today it doesn't matter unless you're you know doing this in massive scales and trying to save you know microseconds off of transactions and things like that so not something for us later learning how to use a commandment but something maybe for another day the other thing I will mention is that for each this is bad but for each is also the alias for for each object terrible on Microsoft's part or whoever created this it just terrible is it very confusing so the thing I would tell you is that the easy way know is right if it's for age and it's at the start of a line it's for h2 statement and so we got to do this whole you know variable and then variables in the variable if it is on the other side of a pipe so in this case right we piped over the process to it then it's going to didn't mean do that it's going to be this particular method for each object and so then we're just going to do the curly braces I'm gonna quit highlighting thanks guys clearly can't be trusted with the mouse and there is a separate video right for for each object and once again I use them pretty interchangeably so just whichever where I'm in the mood for quite frankly alright well I think that covers everything so hopefully that helped you out if you need any more help any more information just let me know thanks have a great day me again hey just reminder if you want to subscribe click on my face over here or if you want to work together or just need a friend hit me up over here or if really what you wanted was more power show videos it's probably it they are over here alright thanks see ya my stop the recording
Info
Channel: Shane Young
Views: 41,761
Rating: undefined out of 5
Keywords: shane young, bold zebras, powershell, foreach, powershell foreach, powershell looping, foreach statement, import-csv, powershell scripting for beginners, powershell tutorial, powershell in a month of lunches, powershell scripting tutorial, foreach powershell, foreach loop, scripting in powershell for beginners, powershell basics, powershell scripting, shane young powershell, bold zebras powershell, powershell basics commands
Id: oCKZ9L-u3wg
Channel Id: undefined
Length: 14min 25sec (865 seconds)
Published: Wed Nov 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.