PowerShell ForEach-Object

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's show we're gonna talk about one of the core powershell commandlets for each object it's a pretty basic command lip but I use it in almost every one of my scripts I want to make sure you understand how it works and what you can do with it but first here's our intro hi my name is Shane Young with bold zebras those guys and today's show we're going to talk about the for each object commandment this would be part of a series or miss can cover individual command let's either I think are really important or I've gotten a lot of questions about on the channel or I just want to make a video today whatever it might be if you're not familiar with for each object it's the commandment that we use to iterate through a set of objects and do something with them right typically grab some of their properties and you know build out information or execute actions and although you know reports we might get all the files won't get off the file system or something like that so for each job makes a very handy command because we use objects so much inside a powershell and it seems like the whole reason we do powershell for the most part is we want to write scripts that'll run over hundreds and thousands of files not just one file at a time right if you just got to edit one file it's probably easier to use the the browser or the mouse or somehow but we're trying to edit lots of stuff we want to do that with you know sets of objects and typically speaking for each objects is one of your go-to tools when you do that so in this video we're gonna look at how to do that so to do that let's get started so switch over to my desktop and I will open our good old friend the PowerShell ISE and you can see that step one I've got here is import CSV and then file dot CSV so let's look at that file real quick so this file is a CSV file that I made it's really complicated I've got three columns right object property name is where I called one column object property color and object property number and then the associate values I made those names for the columns just to try to make it real obvious for you what they are right they can be named column a column B column C or a column chain is awesome whatever you want to do but this is what I went for here so you also see we have three objects in here I did name0 name one and name too the reason for that is when you're working with arrays and when you work when most of the PowerShell seems like instead of starting with one it usually starts with item zero so I want to kind of get you in that habit and remind you that so that's why I named the first object name zero because it is the zero item in the array right cool all right so let's import this file so to do that over here is C we're gonna hit the little button run the selection boom and you can see it brought it in so there they are so now that we know that's a good command I'm just going to take that and I'm gonna put those into a variable called our objects array so we'll grab all that we'll say boom well clear our screen and then type in our objects array like so and we get the same information back but now we've got it a variable you know and we can do things like with our variable like count so boom you can see that there are three objects in there just like we know there is from the file making good progress here all right so let's clear that off now one of the things that I want to do here is I want to show you a bonus tip real quick it doesn't have anything to do with for each object necessarily but it goes to now that we've got all those items in the array remember you can always call those directly right so we can say something like our objects array and then we can do bracket 0 bracket or square brace or what everyone call them alright and so that will actually just return the 0 line I'm write that very first item in there so we can change the 0 to 1 or we could change it to a 2 what happens we change it to a 3 I have nothing in the third the fourth spot which is the third ordinal number s well it's a little crazy just remember arrays start at 0 but that comes in handy because then you can specify this stuff directory right so we can say object are objects and then object property name and boom so we're able to return just the property from a specific item inside of our array not really important for for each object just something add the old tinker when you're trying to get your powershell skills up there right it's a square screen that gets us through all of those pieces so let's take a look at for each object right that is the commandment we're here to learn about so for each object is it name and tail or its name and Tails is it is going to do whatever is inside of the curly braces right so that curly brace and that curly brace one time for each object in the array all right so in our case we're going to pass it we type in our objects all right right and we know if we can we type that in what happens we return three objects so those three objects thinks to the pipe we're gonna send over two for each object so that means it's gonna run whatever is this curly brace three times right once for each object and what's it gonna do it's gonna write to the host right argue about rhino spritz go right to the host so right on the screen the name of the object is right and then dollar sign underscore object property name all right so this is where you got to learn something okay dollar sign underscore well we know we based on the dollar sign that represents a variable right all the powershell variables start with dollar signs but the dollar sign underscore what we like to call that is the fill-in-the-blank variable right because that doesn't have a specific value right if I go down here and type in dollar sign underscore it's like I don't know nothing in there right now right because it's a fill-in-the-blank variable so what that means is that when we're running this stuff in the curly brace it is the current object in the pipe right so we know we're gonna run this three times and so the first time it runs it's gonna run it for the item in the zero location right and then for the item in the first location and then item in the second location so let's run it and see what happens we'll talk about some more highlight that well run it there you go so the name when the object is named zero name one name to right which matches up perfectly with what's in our array name one name zero or name zero name one and name - it's hard to start on zero sometimes so this is really important for you right because this is how you're gonna get and learn and do all the power you just have to remember that when you're inside these curly braces that this is the current object and so then here or doest give me oh let's not do that let's take this current object and let's create one of its properties right or we could call one of its methods because we're working with the actual object we just don't have to specify it by name or by any variable we preset because we're gonna run through here you know in our case three times and each time that we run through it's going to be a different one of the objects based on where we're at in our list kind of makes sense let's do it again just to see all right so let's grab this line and what do we got here so our objects right so dump the three things in our array past those three objects over thanks to the pipe two for each object so we're gonna run the curly braces three times again we're gonna do we're gonna say right host the name of the object is object property name the same thing we did before yay and then - foreground color dollar sign underscore object property color so looking up here object property color we can see that red yellow green so in theory we're gonna write what object wrong and then based on what the value is in this column we're gonna change the text to be that color so let's try this one out and see what happens exactly what we wanted to happen right Oh put that right there for you so name zero is red let's double check ourselves yep name zero was red name one is yellow yep and name two is green so just showing you that you know we can call the individual properties from those objects so we have the ability to do what we need to do all right all right let's try one more example of that and then we'll get into all deeper stuff so real quick here I'm going to specify total numbers of columns equals 0 so this is just a setting where my variables to be 0 right because I use the variable earlier I don't want to have weird numbers so set it back to 0 boom variable 0 let square screen okay our objects array so three objects pass those over to for each object run the stuff in the curly brace three times all the name of the object is our property name okay got that color is the color we've done that all right now here's the new part now we've got a semicolon so what is this semicolon do that says hey I'm done running that line right that right hosts commandment and all of its parameters I'm done with that so then now go and run another command we're still in the curly braces we're still using the current object we're just going to have a new line so we can start something different and what we do with that line we're going to say total number of column equals total number column plus dollar sign underscore dot object property number well what was in that well let's just ask ourselves again by pulling on our right our objects terrain so it's a value so we're and it will take the current total number column which is zero and we're going to add equals zero plus object property number so 100 okay so the first time through it's 100 the second time through it's going to be 100 plus the next values of thousands so it'll be one thousand one hundred got it the third time through it would be total number column it'll be one thousand one hundred plus ten thousand so it's me eleven thousand one hundred right if that math is hard for you I have a different set of videos where I teach math and with somebody all happy help you math too but anyway looks like it's going to be 11 thousand one hundred so let's go back over here let's press the button and run that one so we wrote out to the screen name one name or named zero name one name two and then now if we say total number of number column it is eleven thousand one hundred okay so what am I trying to do here all I'm trying to do with you is reinforce this idea that the is e is annoying when the column is too wide but also that total number column right it's just showing us that we can interact with that property right in a separate line so it had nothing to do with the writing host line but we're running that same we're still in those brackets so dollar sign underscore still the object you were working with and we can still pull its property names in a secondary line good good alright so now that we did that let's look at how will you do that it will better because in reality when I write some of these scripts you know my for each object inside my curly braces I might have a hundred different lines of PowerShell things that you're going to do and now one really wide column that you probably don't want so what we're gonna do here is we're gonna do the same exact thing we're gonna write it a little different make it easier for you and I to work with it and we're gonna show some more advanced moves here so our object array alright so take those three items pipe them over to for each object and then so there's the opening curly brace and so then now what happens is we can put all the other lines down here on their own line it just makes it easier for us to read and write with it a little more readable for you and then just make sure that somewhere right right here and you can see the ISEs help me right it says hey the curly braket opened here and it closes here we're in good shape so make sure you close them this is the first line we're going to write our same line right apparently I liked writing colored text wise making this example the second one we're gonna say I total number column equals total number column object property so same thing when do the math again now we probably want to take this and set it back to zero so we don't mess up let's do that real quick all right now you also notice that remember before we had the semicolon in this line it doesn't matter at this point so if you want to put a semicolon here and then to be trying to make your life easier well not that line but these two lines that's fine but because we put them on a separate line in the ice is e power shell understands that they're new lines so typically because I'm lazy I don't put the semicolons here but a semicolon in that location is it's good it's bad it's indifferent so whatever you're more comfortable with okay so we've got our two lines we did before so this third line here we're going to say if all right now price should make a video and F's at some point we're gonna say if dollar sign underscore object property number so if that property number field is greater than a thousand so looking at these three only name two is greater than a thousand then we're gonna say right host all right we're putting these in Earth's own curly brackets right host this object is bigger than a thousand and then the object property name and close some curly close from curly all right this should be ready to go this is grab it and we'll quarter screen so we don't have any confusion and we'll run that and so you can see it did our line where it wrote out to the host zero it wrote out to the house for one it wrote out the host for two and it said this object is bigger than a thousand and named to so this if statement it got evaluated all three times right but in two of the examples it got skipped and only in the third the last pass was the object creator than a thousand so than it ran and so that went and then like we could also check what is our friend total number column he's eleven thousand one hundred right because we set him back to zero you're writing the whole reason I set him back to zero just in case you didn't get that or if we run this again now what is total number column 22200 right because eleven thousand one hundred was the first number we added a hundred to it we added a thousand to it we added 10,000 to it that's how we had a twenty two thousand two hundred so that's for each object right there's more complicated things you can do with it but I thought I'd make this basic video just to get you through the core pieces of it but last thing I want to mention though is that you will sometimes see with square screen people will take this and they will use the alias for each object right so there is an alias for called for each for for each object don't ever ever ever use that alias though why why because a we told you alias is bad I don't every once you use them anyway but be the biggest reason for that is that there is a for each alias and then there's a separate commandment called for each and so depending on which one you're using the syntax is different it couldn't be more confusing if it tried so now I'm going to explain any further just don't ever use for each as an alias for for each object if you're using for each as a commandment because you're using that as a different set that's great I'll make a later video on how to use for each the standalone command line but for each the alias is the devil as far as I'm concerned so stay away from it alright so I think that gets us through everything today now hopefully you enjoyed this if there's commandlets you want to see videos like this on your just basic breakdowns of single command let let me know happy to make them if I can do anything to help you or write PowerShell scripts for you mentor training just be your friend let's reach out to me and get me a through bold zebras or tweet to me at chains counts also all the commandments and everything we did here will be available down in the description below so check that out cool give me a give me a subscribe over here and 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: 51,029
Rating: undefined out of 5
Keywords: shane young, powershell, bold zebras, Learn powershell, powershell for beginners, foreach-object, foreach, import-csv, powershell array, powershell objects, powershell loops, powershell basics, powershell scripting for beginners, powershell scripting, powershell tutorial, powershell in a month of lunches, shane young powershell, bold zebras powershell, powershell basics commands
Id: BoutUXsBGcU
Channel Id: undefined
Length: 16min 13sec (973 seconds)
Published: Mon Nov 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.