So let's go back into the PowerShell console again.
We'll review the ForEach structure.
And then, of course,
we'll look at how we implement a ForEach enumerator within
PowerShell using some kind of test data.
Okay, so we just spoke about using a ForEach loop.
And this, for me, is one of the most common loops that we would kind of use.
So the first thing we're going to do is actually
create a variable called collection, and we're going to do it this way, 4,
5, 6, 7, 8, 9, and 10.
And just to make sure that we have variables here and values, there we go,
collection, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Now I could have done it using collection 1..10.
It doesn't really make a difference.
Now in order for us to loop through, we're going to use a ForEach command,
which has the something in something, so the evaluation,
and then it has the script block where we execute the commands.
So the way this works, we first declare a variable.
I'm going to call it item,
or we could call it something that kind of makes sense,
so I know it's going to be a number.
So the name of that is really completely up to you.
We then use what's called the in operator,
and then we connect to the collection variable.
And then, of course, we open our script block.
I'm then going to say Write‑Host and just say Current Number,
and then I'll just call my number variable here and close
that out and then do this and then Enter.
And then, of course, as you can see,
it loops down through the numbers and says Current Number 1,
2, 3, 4, 5, 6, 7, 8, 9, 10.
So fairly straightforward to be able to loop.
I mean that just looped through all of the numbers.
Really, really simple.
So let's just clear this again.
Now let's say that we did the same thing again,
collection, but this time we populated it using this approach, so 1..20.
So same thing again.
It's what's referred to as a range operator.
So instead of declaring the actual numbers, we're just going to do a range.
And then, of course, if we just go back through our code here,
I can say ForEach number, write the number out,
and then I'm going to enter that.
And of course, the same thing happens again.
So what's the difference between the two?
Well, nothing,
really because really what happens is when we create a collection of something,
it doesn't have to be, you know, specifically in order.
It could be something like this.
So there's my collection, so I'm going to say collection.
You'll see I have a list of numbers.
And then when I want to create my loop,
so let's go back to the ForEach, Write statement,
close that one out, and then Enter, it'll just write the values.
There's no specific thing here. It's not looking for an order of anything.
So the range operator lets you kind of specify a start
to an end, and it's a logical set.
A collection could be anything again if they wanted to.
Now, of course, we also have the ability, so let me just clear this out.
We also have the ability to be able to use different words,
letters, alphabet, whatever it would be, as part of that collection.
So if we look here, I'm going to change my variable again.
So we'll say collection equals, and then I'm going to
populate it with letters instead, C,
D, E, and F. We'll just do A, B, C, D, E, F. If I then say collection and
render that, it will just give me the letters A, B,
C, D, E, F.
Now if we wanted to iterate through, we can repeat that same ForEach.
Now I'm going to change it from number because it's going to be a letter,
but my variable is still the same.
What I'm then going to do is say Write‑Host The current letter
is: $letter. Now notice, I can't tab this one because I
haven't populated the variable yet.
And then I'll do this and click Enter.
And then it will say the current letter is A, B, C, D, E, F.
So a standard ForEach loop can iterate through any kind of content really.
It really makes no difference whether it's a number,
whether it's a letter, whether it's a word.
So if we were to change that even further,
we could say $collection, and I could say Yes, No,
Maybe.
And when we look at the collection,
It will always treat them as specific individual items.
So the nice thing about the ForEach is that it doesn't really
matter what we're trying to loop through and what we're trying
to do with it. And any of the processing that we want to do, we
can just run it against it.
We don't need to strongly type anything.
We can literally just say here's a bunch of text words, here's a bunch of
letters, here's a bunch of numbers, here's a bunch of files,
etc.
So we have the flexibility of being able to iterate any kind of content by just using the ForEach statement..