1
00:00:01,140 --> 00:00:01,320
Now,

2
00:00:01,320 --> 00:00:04,280
even though we've been looking at taking advantage of the PowerShell

3
00:00:04,280 --> 00:00:07,560
pipeline so that you can do the same action on a bunch of things all

4
00:00:07,560 --> 00:00:10,100
at once, sometimes that isn't an option.

5
00:00:10,100 --> 00:00:14,840
You will encounter situations where you need to process objects one at a time.

6
00:00:14,840 --> 00:00:19,440
For that, you'll use the ForEach‑Object cmdlet.

7
00:00:19,440 --> 00:00:22,800
The name makes it pretty clear. For each thing, do

8
00:00:22,800 --> 00:00:26,640
something with it, or use the object by itself.

9
00:00:26,640 --> 00:00:31,130
The PowerShell syntax for this will use $_ as a

10
00:00:31,130 --> 00:00:34,340
placeholder for each processed object.

11
00:00:34,340 --> 00:00:36,630
Think of that underscore as a fill‑in‑the‑blank.

12
00:00:36,630 --> 00:00:39,580
What you're most likely going to do is run some PowerShell

13
00:00:39,580 --> 00:00:42,180
command or operation using that object.

14
00:00:42,180 --> 00:00:44,470
The results of that operation are written,

15
00:00:44,470 --> 00:00:46,020
then, back to the pipeline,

16
00:00:46,020 --> 00:00:49,540
assuming you're doing something that writes an object back to the pipeline.

17
00:00:49,540 --> 00:00:51,940
And here's what it looks like in PowerShell.

18
00:00:51,940 --> 00:00:53,110
In this example,

19
00:00:53,110 --> 00:00:56,720
I'm taking the numbers 1 through 10. That dot dot

20
00:00:56,720 --> 00:01:00,410
you see there between the numbers, that is the range operator,

21
00:01:00,410 --> 00:01:04,440
which will write all the numbers between 1 and 10 to the pipeline.

22
00:01:04,440 --> 00:01:06,280
For each number,

23
00:01:06,280 --> 00:01:10,860
I want to multiply it by 2. All right, there's no cmdlet that does that for me,

24
00:01:10,860 --> 00:01:13,560
so I'll have to do that individually,

25
00:01:13,560 --> 00:01:16,200
one at a time, for every number that comes through the

26
00:01:16,200 --> 00:01:18,710
pipeline. The part that you see here with the curly

27
00:01:18,710 --> 00:01:21,240
brackets is called a script block.

28
00:01:21,240 --> 00:01:27,140
PowerShell will execute the code and write the results to the pipeline.

29
00:01:27,140 --> 00:01:32,140
The $_ means the current object in the pipeline.

30
00:01:32,140 --> 00:01:34,450
This is where being able to visualize what PowerShell

31
00:01:34,450 --> 00:01:36,240
is doing can be really helpful.

32
00:01:36,240 --> 00:01:38,620
So the first object that's coming through the pipeline is the

33
00:01:38,620 --> 00:01:44,870
number 1. PowerShell puts 1 in places where $_ is and

34
00:01:44,870 --> 00:01:51,240
multiplies it by 2. The result being 2, which is then passed onto the pipeline.

35
00:01:51,240 --> 00:01:54,030
Then PowerShell gets the next number, 2, and plugs

36
00:01:54,030 --> 00:01:57,440
that in; 2 times 2 is 4, and so on.

37
00:01:57,440 --> 00:01:59,980
The process continues serially, you know,

38
00:01:59,980 --> 00:02:04,160
one at a time, until all the incoming objects have been processed.

39
00:02:04,160 --> 00:02:04,690
And, yes,

40
00:02:04,690 --> 00:02:07,510
you'll typically have more complex things to do in that script

41
00:02:07,510 --> 00:02:11,000
block other than multiplying by a number, but this is a good

42
00:02:11,000 --> 00:02:13,830
example that illustrates a $_,

43
00:02:13,830 --> 00:02:17,640
which admittedly can be confusing for a lot of PowerShell beginners.

44
00:02:17,640 --> 00:02:21,530
PowerShell 7 introduced a new feature to ForEach‑Object.

45
00:02:21,530 --> 00:02:23,560
Now, the overall concept hasn't changed.

46
00:02:23,560 --> 00:02:27,040
You're still doing something with each individual object.

47
00:02:27,040 --> 00:02:31,130
We still have a $_ in the script block,

48
00:02:31,130 --> 00:02:34,340
so that is still getting piped in and processed.

49
00:02:34,340 --> 00:02:35,470
However,

50
00:02:35,470 --> 00:02:39,850
now you can tell PowerShell to run that script block in parallel. So

51
00:02:39,850 --> 00:02:43,370
this is great when you have a lot of things to process or the script

52
00:02:43,370 --> 00:02:46,240
block itself might take a long time to run.

53
00:02:46,240 --> 00:02:49,510
So instead of processing one at a time sequentially,

54
00:02:49,510 --> 00:02:52,940
you can process multiple objects at the same time.

55
00:02:52,940 --> 00:02:54,390
Now the default is 5,

56
00:02:54,390 --> 00:02:56,780
but you can scale that up or down as you need to

57
00:02:56,780 --> 00:02:59,340
with the throttle limit parameter.

58
00:02:59,340 --> 00:03:01,840
But let me caution you; without getting into the technical

59
00:03:01,840 --> 00:03:04,970
details, using this parallel feature does incur some

60
00:03:04,970 --> 00:03:07,240
additional processing overhead.

61
00:03:07,240 --> 00:03:11,680
Don't assume that parallel means faster. For a small number of

62
00:03:11,680 --> 00:03:16,390
objects, or actions that can run quickly, using the ‑parallel

63
00:03:16,390 --> 00:03:18,540
actually might be a little slower.

64
00:03:18,540 --> 00:03:19,890
Now, with experience,

65
00:03:19,890 --> 00:03:23,570
you'll be able to gauge where and when to use this feature

66
00:03:23,570 --> 00:03:27,280
and see if it's warranted. In my example here, if I'm

67
00:03:27,280 --> 00:03:29,610
getting 5000 event log entries from, say,

68
00:03:29,610 --> 00:03:33,300
50 servers, using ‑parallel will almost certainly be

69
00:03:33,300 --> 00:03:37,760
faster. Now for the totally confusing part. The

70
00:03:37,760 --> 00:03:42,340
ForEach‑Object cmdlet has an alias of foreach.

71
00:03:42,340 --> 00:03:44,400
When you're banging that code in the console prompt,

72
00:03:44,400 --> 00:03:48,540
like in my slide examples, this really shouldn't be an issue.

73
00:03:48,540 --> 00:03:53,140
However, PowerShell also has a foreach keyword.

74
00:03:53,140 --> 00:03:56,440
This usage requires a different syntax.

75
00:03:56,440 --> 00:04:00,740
Now you're most likely to use the foreach keyword in your PowerShell scripts,

76
00:04:00,740 --> 00:04:05,240
although there's nothing preventing you from using it in a PowerShell prompt.

77
00:04:05,240 --> 00:04:08,380
PowerShell can figure out which you are using,

78
00:04:08,380 --> 00:04:12,740
whether it's a foreach keyword or the foreach alias.

79
00:04:12,740 --> 00:04:14,800
Now, I'm not going to dive into the keyword,

80
00:04:14,800 --> 00:04:17,950
since I think most of that is a scripting technique and we

81
00:04:17,950 --> 00:04:26,000
really won't focus on that right now. But if you want to jump ahead, take a look at the about foreach help topic.

