Understanding For Loops In PowerShell
Think you can do better? Show us!
For loops are one type of loop in PowerShell to use when you know how many times you’d like to loop. In this video, Adam goes over how to create a for loop and covers how it works. If you enjoyed this video, be sure to head over to techvideos.io to get free access to our entire library of content!
Although this snip, I would like to talk about giving a brief introduction to for loops in power shell, so there's a few different kinds of loops in powerShell we have for loops for each loops do, do while we're all going to be discussing them over the course in a few steps in this instance, we're going to use for a for loop. So what's the difference between a for loop and another kind of loop will a for loop? Is we need to run an expression or do something X number of times so a certain number of times so. Let's say you know, I have a really stupid example here doing write host and doing one through 5 very simple example on just writing 12345 to the console. I'm doing that 5 different times and I know that I want to iterate over these things 5 different times the for each loop runs through if you're familiar with the for each loop the for each loop iterates over each element. Regardless of how many are in there for loop is a great way to define if you know ahead of time? How many. Iteration that you want to do so you see their on line 9 that I have an example of a for loop. So a for loop has an integer which is an I normally. It's an I in the programming world could be anything equals an integer and that equals on line 13, there I have at equaling to one that's typical doesn't necessarily have to be that's what the integer is going to start out with that's what the integer is going to start out what's going to start out with the value one. Then you have a semicolon, then it's going to have the expression to run while it equals true so what's going to happen. Here is that while I whatever that integer Is is less than 6 their on line 13. Then it will keep running and running and running and running in order to prevent an infinite loop. We have the finally there is the integer Increment, so notice there that I'm on line 13. I am doing dollar I plus plus that increments dollar I. So when I run this. Knows that we have the same thing 12345, but it stopped because the expression here to run was less than 6 if I would change this to say. I don't know 10 or something like this now notice. What happens, it's one through 9. So we're able to figure out how many iterations that run through demonstrate the difference here to let's say that I start out my integer with 4 What do you think is going to happen? It's going to give you 4 through 9 instead of one through 9. Like it did before so allows you to change, these things up. Now we don't this is common to use an integer to Increment by one. But we don't necessarily have to well. This isn't common but I want to actually see if this works or not so let's say we do, I equals I +2. I don't know if this is I don't even know if this is going to work or not, but you'll be here to wait. So yeah, that actually works too. I've never actually done that. But it doesn't work, then we increment it by 2. 46 and 8 change this to one again and it's going to do it again 13579 week and that's a way we can do odds and Evens as well really anything could be in here, but however. Let's try. This I've never tried this before either. Let's say that this is I don't know true Boolean true is always going to be Boolean True? What do you think is going to happen now let's find out now notice that I'm set in a perpetual loop? I is going to be one forever and ever and ever and ever and ever and it's just going to go on forever until my. Computer crashes probably so. It's not a good thing don't do that, but that is a good example of you can put anything here in the expression that you want like I said typically it's going to be, I plus plus or whenever you're iterator is and now we're back to. How we're supposed to be all is good in the world and his back to 45678 night so a for loop is great for those times. When do you want to run a command or something a set number of times so that concludes the snip on understanding for loops in powershell.