PowerShell ForEach Deep Dive
2K views
Aug 21, 2024
Harness the power of the PowerShell ForEach loop! This video covers three ways to iterate through an array and the differences in their performance. ? Get the Code Here! ? https://github.com/JeffBrownTech/jeffbrowntech_youtube/blob/main/2024-07-22-PowerShellForEach/PowerShellForEach.ps1 ? Read More About This ? PowerShell ForEach: Everything You Need to Know https://jeffbrown.tech/powershell-foreach/ ? Watch: ? PowerShell Begin Process End Block Demystified https://youtu.be/RdnQbA3U7HQ?si=sAXhefJCtEnfxW9L ⏰ Chapters ⏰ 00:00 Introduction 00:27 foreach statement 02:21 ForEach-Object cmdlet 06:46 ForEach method 07:27 ForEach performance testing 10:19 Summary
View Video Transcript
0:00
What's up everyone
0:01
Today we're going to talk about the PowerShell for each statement. We have a couple of different ways to use for each in PowerShell and not all of them
0:08
are created equally. Stay tuned to the end of the video to find out performance differences between each one
0:14
And if you happen to know why each of them perform differently, please leave me a comment down below
0:19
As always, this code is available out in my GitHub repository and I'll leave a link to it down in
0:24
the video description. Let's go ahead and get started. The first one we have is the For Each statement
0:29
It starts here on line 2 in our definition. You can see it's for each and then parentheses we have dollar sign item and dollar
0:36
sign collection. We have some curly brackets and then the statements we want to go inside of our loop
0:41
Now typically the collection variable here is already defined earlier in your script
0:45
You can generate it in the for each statement. We'll see an example of that below
0:50
But the dollar sign item variable is whatever you want to call it in that moment
0:54
Typically you might see something like all users so your dollar sign item would be
0:59
user so you would see for each user in all users but really the first part of that for each is a
1:06
variable you can name it whatever you want to when you're looping through the for each you'll
1:11
make reference to the current item in your four each using that variable name let's go ahead and see
1:16
an example here on line seven i'm defining an array called names array and it has four different names in
1:22
it so we'll go ahead and get this into our session and then on line 10 we have our for each statement
1:27
defined we're saying for each name and names array and inside of our script block we're just
1:32
outputting the current item in our collection using dollar sign name. If we go ahead and run this we can see each name output to the screen
1:41
As I mentioned you can create an array or a collection in the for each statement
1:45
We see an example of out on line 13. We're saying for each process and get process so we're running the get process command as part
1:51
of our collection that we want to iterate through and it will generate that on the fly and then
1:56
inside of our script block, we're running an if statement. We're saying if process, meaning the current process in our loop here
2:03
We're looking at the process name property of the current process starts with an A
2:09
Then we're going to output that process name. So we go ahead and highlight this
2:14
And we can see I have a couple of services here that start with A. There's the only ones output it to the screen
2:21
The second example of 4Each is using for each dash object command
2:25
I believe you'll typically see this used in the pipeline. For example, on line 21, we're running Git Process, then we have pipeline, and then we're saying for each object
2:34
and then what we want to do with each object coming through the pipeline
2:38
One thing to point out I got it here on line 24 There is an alias for the for each object command and that is actually for each But I don want you to confuse line 24 here the four each here as being the same as the one we just took a look at up here
2:55
They are not the same thing. So if you see for each object or for each in the pipeline like this, just know it's an alias and they are the same thing
3:03
Let's take a look at how to use for each in the pipeline. You usually see it with script blocks
3:08
Again, this is just a revamp of what we did earlier. We're running Git Process, running it through the pipeline of For Each Object
3:16
and inside of our script block, we're saying if the process name starts with an A
3:19
we're going to output it. The big difference here is you don't have a variable name like we did up here with name or process
3:26
to indicate the current item in the For Each Loop. Here you have just Dollar Sign underscore that's referring to the current object in your pipeline
3:36
that you're working with. And if we run this, we'll get the exact same output that we saw before
3:44
You can also use begin, process, and end blocks with the for-each object command
3:48
I have another video that goes over how these work. I'll include a link to that down below
3:53
In this example, we have an array of numbers of 1 through 5, running it through the pipeline to 4-each object
4:00
In our begin block, we're going to say starting. Then we're going to process each item, and then we're going to have an end block saying ending
4:06
If you don't know how these script blocks work, the begin one should execute first and only once
4:13
The process script block will execute however many times or objects there are in our pipeline
4:19
Then the end block will execute just once at the very end. Let's take Align 35 here and execute it
4:26
So we see starting, 1, 2, 3, 4, 5, and then ending. I believe if you don't specify any of these, it just defaults to the process block
4:35
much like we see here in our earlier example. You can also use something called
4:41
operations statements with the for each object. In this case, we're running Git Process for each object
4:46
and we're just outputting the process name. Process name is just a property on our list of process objects
4:52
If we output that, you can see it just outputs every process name inside of our pipeline
4:59
And then you can also perform methods on your incoming objects. In this example, I have a single
5:05
string of 1, 2, 3, 4, and it's separated by commas. What we can do is run that through the 4-H
5:11
object. We're going to access the split method, which is available on string data types
5:16
and we're going to pass it the comma, and it's going to automatically split our single string
5:22
into an array of strings And there we go it outputs 1 2 3 4 One really cool feature that PowerShell 7 introduced is parallel processing with the for each block
5:35
In this case, we have an array of numbers of 1 through 12
5:39
We're saying for each object, and now we have a new parameter called parallel
5:43
Inside of its script block, we're saying processing dollar sign underscore, which just means the current object
5:49
in our pipeline. And then we're going to sleep for three seconds
5:54
And then we also have a throttle limit of four. So this means it's going to take the first four objects coming into our pipeline
6:01
Process those and it's going to go to the next ones and then the next ones. In this case, we should see one, two, three, four, and a pause for three seconds
6:10
Five, six, seven, eight, pause, nine, ten, eleven, twelve, and then a pause
6:14
As it processes through each of its limits here. Let's go ahead and execute this and we should see that
6:20
one, two, three, four, and a pause, five, six, seven, eight, and a pause
6:25
nine, ten, eleven, twelve, and another three second pause. So it's really great if you're needing to process things on maybe a list of computers
6:34
you're running an update or need to make some type of change on it. You don't have to wait for it to iterate through each one
6:39
You can set a throttle limit and have it execute on multiple items at the same time
6:46
Our last for each option is the for each method. This is a method that is included on arrays
6:53
Go ahead and re-initialize our names array here with our four names. What we can do is just say names array
6:58
That's how you access the method for the array data object there
7:04
And inside the parentheses and inside a script block, just saying dollar sign underscore
7:08
again, that's just the current object that we have there that we're going to iterate over
7:12
So if we do that, it's going to say, yes, our four names. And then do some additional things here
7:17
exact same thing I'm just going to say my name is and then dollar sign underscore just showing you know some string
7:23
interpolation there and it outputs our results there as I indicated at the beginning of the video
7:29
we have these three different for each methods here but each one is not created equally and are
7:34
pretty different in terms of their performance let's go ahead and define a very large array here
7:39
of one through you know 99 million I can't remember how big this number is but we're going to
7:45
define our variable here. And we're going to test the performance of each method. This first
7:52
one is the for each statement. We're going to do measure command and inside of our script block
7:58
here. We'll say for each item in numbs, which is our very large array of numbers, and just
8:03
output it to the screen And we see how long this takes here Okay that was pretty good here Just two seconds Let go ahead and move to our next method
8:17
We have the for each method where we're going to say dollar sign numbs and then dot for each
8:22
and then again just output it to the screen with dollar sign underscore
8:26
Let's go ahead and get that started and see how long it takes. Okay, definitely longer
8:41
That was 13 seconds, so 11 more seconds than our other method. And what I'm going to do here, I know this one takes the longest, so I'm going to go ahead
8:48
and start executing it. But here we're running the for each object
8:52
We have dollar sign numbs. We're piping it over to four each object, and then just outputting it to the screen again
8:58
not really doing any type of calculation, manipulation or anything like that. And that took 17 seconds
9:08
Like I said, I knew this one was going to take the longest. So even just outputting to the screen here, not performing anything in it
9:15
we can see how quickly it runs through each of these four-each methods
9:19
Again, the for-each object in terms of using it in the pipeline was 17 seconds
9:25
The for-each method was 13, and then just the for-each statement. Our first one here on line 67 took two seconds to iterate through
9:34
So this is pretty eye-opening for me. first learned that. I always use the for each object example here because I just didn't want to
9:40
come up with a variable name. I like just using dollar sign underscore. But when I came across the
9:45
performance differences of these, I started going through and using for each item and items here
9:52
I've seen a stack overflow discussion. If I can find it again, I'll link down in the video description
9:56
I think it has to take all these numbers and then start sending them through the pipeline
10:01
And that's why it takes so long versus line 67, just the for each statement. It already has
10:06
all the collection there and it just starts iterating through all of them immediately. But again, if you happen to know the real reasons why these have different performance capabilities
10:15
please leave me a comment down below. I think it'll be a good discussion and for other people to learn
10:19
Anyway, that does it for this video. Hope you learn something new. Hopefully you can change your scripts to maybe use for each
10:26
Or if you use for each object, know there is a parallel option here inside of PowerShell 7
10:31
That does it for this video. Thank you for watching and we'll see you next time
#Computers & Electronics
#Programming
#Software