Another option that we have for enumeration is what's
referred to as the foreach numerator.
So the first question would be what is a foreach enumerator?
Well,
the foreach statement is a scripting language construct for
iterating through collections of items.
You can then execute operations against each item that you found,
and then you can process each item within the collection
unless obviously you change the execution path.
So what is in a for loop statement or a foreach loop statement?
Well, the construct for each for loop statement is straightforward.
It basically says foreach item in a collection, perform the specified task.
So of course, the first thing in there is an item.
So this is the variable that contains the current
item that we're iterating through.
The second is the collection that we're getting the items from.
So you're going to say foreach item in the collection,
the collection could be numbers, objects,
files, etc.
And then, of course, we have a script block,
which is what happens when we've retrieved a specific item.
So what does that look like in code?
Well, the foreach base syntax is as simple as this.
It's foreach (Item In Collection),
so item in the collection is in the parentheses, so that's the evaluation.
And then, of course, we have the script block,
which is the code or the sequence of commands that need to execute.
So if we create a variable here, which basically just has numbers 1 to 10,
so it's effectively an array,
I can then do a foreach statement and say foreach item, notice it's declared as
a variable, so it's an inline variable, in the collection,
which is the declared variable, and then,
of course, I can write the value or perform some action on those values.
Now, if we're looking at the syntax for this a little bit more,
let's say we had three different types because,
of course, when we're iterating through,
it doesn't have to just be numbers.
So I have collection1,
which is numbers 1, 2, 3, 4, 5, all the way up to 10,
collection2 is a series of letters,
and then path equals the path to a specific file location.
If we were to look at the number syntax,
it would be similar to what we just looked at,
so foreach item,
as in each number in the collection, go and get that
number and then write the value.
If I was looking at the letters, the same principle would apply.
So foreach item, the syntax doesn't change.
You don't reference a different type of object
unless you've declared it that way.
But it basically just iterates.
And then, of course, this time around, if we're looking at files,
I can combine other things together.
So I can say foreach file,
remember that file and item are just arbitrary names for the variables.
They only make sense when you call them later on.
And then, of course, for this one,
I want to use the path, but I can't just call path
directly, so I have to use Get‑ChildItem,
which is a regular out‑of‑the‑box PowerShell Command,
which will then retrieve me an array object of all
of the files in that directory.
And then, at that point,
I can just say Write‑Host Current Filename. So the foreach one is a common one.
I use it a lot when we're iterating through various collections of data.
It's nice and quick. It's easy to use.