Understanding ForEach Loops In PowerShell
Think you can do better? Show us!
PowerShell has a few different kinds of foreach loops from the foreach statement, the ForEach-Object cmdlet and the foreach method. In this video, Adam covers them all and compares how they all work.
All right in this techsnip we're going to talk about understanding for each loops in PowerShell. Now the loop construct is very common, and you'll use this a lot, but it's not one thing that a lot of beginners start out with so to demonstrate why, why you would want to use a for each loop or any loop in the 1st place notice there on line 2 through 4. I have 3 different commands. I am adding content to a bunch of different files and I'm adding the same value to 3 different files so one thing that you need to lookout for when your scripting or programming in general is to be dry. And that’s meant to be don't repeat yourself, so notice that I am actually calling the same command. Add content 3 times with the same value on each of those that's a clear indication that we need a loop. But what's a for each loop, well a for each loop allows us to do the same action, Do the same expression multiple times on, The same a set of value. So to demonstrate that so let's say that looking up here again. What is the difference between each of these there's no difference in the command any of the parameters except for the path parameter so the path parameter has 3 different values. Their C folder file text program files and folder 3 they're different. To convert this to a for each loop a for each loop allows us to just run at add content and just dynamically put in what's different it allows us to eliminate all that redundancy. So to do that, we first need to bring in all these paths into an array. So we bring in all of the elements that are going to be different in this case, we have 3 file paths. So now we have 3 file paths in an array so that all works great. So how do you do a for each statement well a for each statement first of all is we have different for each loops. We have a for each statement in powershell. I have a for each objects cmdlet the first thing I'm going to talk about is the for each statement, then notice their on line 11. I kind of have a framework of the for each statement, the for each statement has an iterator and it has the collection that you're going to be actually looping through we call this looping through iterating over each of those. Elements in an array so we have the for each statement, then we have the close open paren and close paren, and inside there we have the iterator value it’s going to be the actually the iterator variable, which can be anything normally. It's it could be dollar I, could be dollar food, doesn't matter. The variable name and then we have the word in which is in whatever collection. It is this in this instance, we're iterating over an array and then on line 12 that we have. Code goes here iterator is available in here only so the iterator variable that will only expand inside of that for each loop. So let me show you an example of what this is so let's say that I have my path so paths has my 3 file paths in there and let's say that I want to read each of those I can reuse those with the for each loop so that worked by powerShell was able to read each of those values in paths and assign each of those elements one-at-a-time the dollar I variable but like I said, you don't have to use dollar I. dollar I is a common one in the programming world. But you can use path to just the singular form of path and that will work as well. The variable does not matter. Now that we had that for each loop we know that it's iterating over each of those at this point, I can then add run some kind of command in here, and reference Those values so this instance. I'm just going to use for each dollar, the dollar I in that collection, or that those paths array and then I'm going to call add content. So now that did exactly the same thing as before, and notice that I only use 3 lines here. I only 3 lines up here and this is a bad example. I guess but I could have used 50 up here and still use 3, so this is a lot cleaner way to perform an action on items in the collection. So next step is it's a little bit. PowerShell will leave you confused at this at first is that you have for the for each statement and the for each object cmdlet. And these do just about the same thing. They’re a little different under the hood. But for the beginner you probably don't need to worry about that, so let's say that I just ran this. I just have paths my array piped it to for each object so every one of those elements inside of path so all my different file paths each of those getting piped to the for each object cmdlet and then the for each objects. cmdlet has a process parameter. Which then is essentially the same thing as What I have here on line 22 it's just about exactly the same thing. There's some stuff under the hood and like I said it's different. But for all intents and purposes for the beginner just about the same thing. What ever you think is more intuitive personally I would use the for each statement over there for each cmdlet because it's a little bit faster, once you get started with some big arrays. But that's for another day that is the for each object cmdlet and for each object cmdlet has a lot of. Other parameters on here that you can play with like begin processing if your familiar with the pipeline. It has begin processing in remaining scripts member name. There's a lot of different parameters on here we’ll use but feel free to RT, RTFM I guess, and use the use the help and go to help for each object, and it will go through all those parameters for you alright so finally we have the for each method, so to make it even more fun in powershell version 4. They released a for each method and the for each method is actually a method on collection so how that works is since the method. We use dot notation to append the dot and then the name of the method so in this case, we have paths here and we use this by appending dot for each and then we have a open paren close paren like we do all methods and then we have a script block in between here this again. This does the exact same thing as the other ones but what you'll find is in this instance. It's much, much faster under the hood. They made some improvements in powerShell version four the for each method is much faster to use there's a few quirks, and things that we may get into on later snips there more more advanced. But if you're looking if you have a very large array like for example, if paths was I don't know 100 thousand elements or something and you do a performance test the for each method is typically the fastest because it's a method directly on the object itself. so that was a. A brief introduction to for each methods in powershell, hope it was worthwhile.