Now,
even though we've been looking at taking advantage of the PowerShell
pipeline so that you can do the same action on a bunch of things all
at once, sometimes that isn't an option.
You will encounter situations where you need to process objects one at a time.
For that, you'll use the ForEach‑Object cmdlet.
The name makes it pretty clear. For each thing, do
something with it, or use the object by itself.
The PowerShell syntax for this will use $_ as a
placeholder for each processed object.
Think of that underscore as a fill‑in‑the‑blank.
What you're most likely going to do is run some PowerShell
command or operation using that object.
The results of that operation are written,
then, back to the pipeline,
assuming you're doing something that writes an object back to the pipeline.
And here's what it looks like in PowerShell.
In this example,
I'm taking the numbers 1 through 10. That dot dot
you see there between the numbers, that is the range operator,
which will write all the numbers between 1 and 10 to the pipeline.
For each number,
I want to multiply it by 2. All right, there's no cmdlet that does that for me,
so I'll have to do that individually,
one at a time, for every number that comes through the
pipeline. The part that you see here with the curly
brackets is called a script block.
PowerShell will execute the code and write the results to the pipeline.
The $_ means the current object in the pipeline.
This is where being able to visualize what PowerShell
is doing can be really helpful.
So the first object that's coming through the pipeline is the
number 1. PowerShell puts 1 in places where $_ is and
multiplies it by 2. The result being 2, which is then passed onto the pipeline.
Then PowerShell gets the next number, 2, and plugs
that in; 2 times 2 is 4, and so on.
The process continues serially, you know,
one at a time, until all the incoming objects have been processed.
And, yes,
you'll typically have more complex things to do in that script
block other than multiplying by a number, but this is a good
example that illustrates a $_,
which admittedly can be confusing for a lot of PowerShell beginners.
PowerShell 7 introduced a new feature to ForEach‑Object.
Now, the overall concept hasn't changed.
You're still doing something with each individual object.
We still have a $_ in the script block,
so that is still getting piped in and processed.
However,
now you can tell PowerShell to run that script block in parallel. So
this is great when you have a lot of things to process or the script
block itself might take a long time to run.
So instead of processing one at a time sequentially,
you can process multiple objects at the same time.
Now the default is 5,
but you can scale that up or down as you need to
with the throttle limit parameter.
But let me caution you; without getting into the technical
details, using this parallel feature does incur some
additional processing overhead.
Don't assume that parallel means faster. For a small number of
objects, or actions that can run quickly, using the ‑parallel
actually might be a little slower.
Now, with experience,
you'll be able to gauge where and when to use this feature
and see if it's warranted. In my example here, if I'm
getting 5000 event log entries from, say,
50 servers, using ‑parallel will almost certainly be
faster. Now for the totally confusing part. The
ForEach‑Object cmdlet has an alias of foreach.
When you're banging that code in the console prompt,
like in my slide examples, this really shouldn't be an issue.
However, PowerShell also has a foreach keyword.
This usage requires a different syntax.
Now you're most likely to use the foreach keyword in your PowerShell scripts,
although there's nothing preventing you from using it in a PowerShell prompt.
PowerShell can figure out which you are using,
whether it's a foreach keyword or the foreach alias.
Now, I'm not going to dive into the keyword,
since I think most of that is a scripting technique and we
really won't focus on that right now. But if you want to jump ahead, take a look at the about foreach help topic.