1
00:00:00,040 --> 00:00:02,420
So let's go back into the PowerShell console again.

2
00:00:02,420 --> 00:00:04,350
We'll review the ForEach structure.

3
00:00:04,350 --> 00:00:05,140
And then, of course,

4
00:00:05,140 --> 00:00:08,350
we'll look at how we implement a ForEach enumerator within

5
00:00:08,350 --> 00:00:14,040
PowerShell using some kind of test data.

6
00:00:14,040 --> 00:00:17,740
Okay, so we just spoke about using a ForEach loop.

7
00:00:17,740 --> 00:00:22,040
And this, for me, is one of the most common loops that we would kind of use.

8
00:00:22,040 --> 00:00:24,300
So the first thing we're going to do is actually

9
00:00:24,300 --> 00:00:31,440
create a variable called collection, and we're going to do it this way, 4,

10
00:00:31,440 --> 00:00:33,840
5, 6, 7, 8, 9, and 10.

11
00:00:33,840 --> 00:00:37,630
And just to make sure that we have variables here and values, there we go,

12
00:00:37,630 --> 00:00:39,890
collection, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

13
00:00:39,890 --> 00:00:43,080
Now I could have done it using collection 1..10.

14
00:00:43,080 --> 00:00:44,840
It doesn't really make a difference.

15
00:00:44,840 --> 00:00:49,790
Now in order for us to loop through, we're going to use a ForEach command,

16
00:00:49,790 --> 00:00:54,550
which has the something in something, so the evaluation,

17
00:00:54,550 --> 00:00:58,740
and then it has the script block where we execute the commands.

18
00:00:58,740 --> 00:01:01,490
So the way this works, we first declare a variable.

19
00:01:01,490 --> 00:01:02,700
I'm going to call it item,

20
00:01:02,700 --> 00:01:05,670
or we could call it something that kind of makes sense,

21
00:01:05,670 --> 00:01:07,490
so I know it's going to be a number.

22
00:01:07,490 --> 00:01:10,840
So the name of that is really completely up to you.

23
00:01:10,840 --> 00:01:13,990
We then use what's called the in operator,

24
00:01:13,990 --> 00:01:18,740
and then we connect to the collection variable.

25
00:01:18,740 --> 00:01:21,040
And then, of course, we open our script block.

26
00:01:21,040 --> 00:01:26,570
I'm then going to say Write‑Host and just say Current Number,

27
00:01:26,570 --> 00:01:31,970
and then I'll just call my number variable here and close

28
00:01:31,970 --> 00:01:35,840
that out and then do this and then Enter.

29
00:01:35,840 --> 00:01:37,260
And then, of course, as you can see,

30
00:01:37,260 --> 00:01:40,800
it loops down through the numbers and says Current Number 1,

31
00:01:40,800 --> 00:01:43,040
2, 3, 4, 5, 6, 7, 8, 9, 10.

32
00:01:43,040 --> 00:01:45,270
So fairly straightforward to be able to loop.

33
00:01:45,270 --> 00:01:47,380
I mean that just looped through all of the numbers.

34
00:01:47,380 --> 00:01:49,740
Really, really simple.

35
00:01:49,740 --> 00:01:51,540
So let's just clear this again.

36
00:01:51,540 --> 00:01:54,470
Now let's say that we did the same thing again,

37
00:01:54,470 --> 00:02:00,210
collection, but this time we populated it using this approach, so 1..20.

38
00:02:00,210 --> 00:02:01,560
So same thing again.

39
00:02:01,560 --> 00:02:03,710
It's what's referred to as a range operator.

40
00:02:03,710 --> 00:02:08,340
So instead of declaring the actual numbers, we're just going to do a range.

41
00:02:08,340 --> 00:02:10,890
And then, of course, if we just go back through our code here,

42
00:02:10,890 --> 00:02:14,450
I can say ForEach number, write the number out,

43
00:02:14,450 --> 00:02:16,190
and then I'm going to enter that.

44
00:02:16,190 --> 00:02:19,040
And of course, the same thing happens again.

45
00:02:19,040 --> 00:02:20,670
So what's the difference between the two?

46
00:02:20,670 --> 00:02:21,270
Well, nothing,

47
00:02:21,270 --> 00:02:25,700
really because really what happens is when we create a collection of something,

48
00:02:25,700 --> 00:02:31,370
it doesn't have to be, you know, specifically in order.

49
00:02:31,370 --> 00:02:33,260
It could be something like this.

50
00:02:33,260 --> 00:02:36,190
So there's my collection, so I'm going to say collection.

51
00:02:36,190 --> 00:02:37,640
You'll see I have a list of numbers.

52
00:02:37,640 --> 00:02:41,710
And then when I want to create my loop,

53
00:02:41,710 --> 00:02:47,160
so let's go back to the ForEach, Write statement,

54
00:02:47,160 --> 00:02:50,320
close that one out, and then Enter, it'll just write the values.

55
00:02:50,320 --> 00:02:53,940
There's no specific thing here. It's not looking for an order of anything.

56
00:02:53,940 --> 00:02:56,930
So the range operator lets you kind of specify a start

57
00:02:56,930 --> 00:02:58,940
to an end, and it's a logical set.

58
00:02:58,940 --> 00:03:03,440
A collection could be anything again if they wanted to.

59
00:03:03,440 --> 00:03:07,380
Now, of course, we also have the ability, so let me just clear this out.

60
00:03:07,380 --> 00:03:12,230
We also have the ability to be able to use different words,

61
00:03:12,230 --> 00:03:16,440
letters, alphabet, whatever it would be, as part of that collection.

62
00:03:16,440 --> 00:03:19,870
So if we look here, I'm going to change my variable again.

63
00:03:19,870 --> 00:03:22,970
So we'll say collection equals, and then I'm going to

64
00:03:22,970 --> 00:03:30,740
populate it with letters instead, C,

65
00:03:30,740 --> 00:03:40,510
D, E, and F. We'll just do A, B, C, D, E, F. If I then say collection and

66
00:03:40,510 --> 00:03:42,630
render that, it will just give me the letters A, B,

67
00:03:42,630 --> 00:03:44,740
C, D, E, F.

68
00:03:44,740 --> 00:03:50,300
Now if we wanted to iterate through, we can repeat that same ForEach.

69
00:03:50,300 --> 00:03:53,560
Now I'm going to change it from number because it's going to be a letter,

70
00:03:53,560 --> 00:03:55,940
but my variable is still the same.

71
00:03:55,940 --> 00:04:03,820
What I'm then going to do is say Write‑Host The current letter

72
00:04:03,820 --> 00:04:09,520
is: $letter. Now notice, I can't tab this one because I

73
00:04:09,520 --> 00:04:11,810
haven't populated the variable yet.

74
00:04:11,810 --> 00:04:14,240
And then I'll do this and click Enter.

75
00:04:14,240 --> 00:04:17,340
And then it will say the current letter is A, B, C, D, E, F.

76
00:04:17,340 --> 00:04:23,530
So a standard ForEach loop can iterate through any kind of content really.

77
00:04:23,530 --> 00:04:25,670
It really makes no difference whether it's a number,

78
00:04:25,670 --> 00:04:28,550
whether it's a letter, whether it's a word.

79
00:04:28,550 --> 00:04:30,910
So if we were to change that even further,

80
00:04:30,910 --> 00:04:39,040
we could say $collection, and I could say Yes, No,

81
00:04:39,040 --> 00:04:40,740
Maybe.

82
00:04:40,740 --> 00:04:42,690
And when we look at the collection,

83
00:04:42,690 --> 00:04:46,800
It will always treat them as specific individual items.

84
00:04:46,800 --> 00:04:49,580
So the nice thing about the ForEach is that it doesn't really

85
00:04:49,580 --> 00:04:51,870
matter what we're trying to loop through and what we're trying

86
00:04:51,870 --> 00:04:55,180
to do with it. And any of the processing that we want to do, we

87
00:04:55,180 --> 00:04:56,450
can just run it against it.

88
00:04:56,450 --> 00:04:58,200
We don't need to strongly type anything.

89
00:04:58,200 --> 00:05:01,750
We can literally just say here's a bunch of text words, here's a bunch of

90
00:05:01,750 --> 00:05:04,660
letters, here's a bunch of numbers, here's a bunch of files,

91
00:05:04,660 --> 00:05:05,160
etc.

92
00:05:05,160 --> 00:05:12,000
So we have the flexibility of being able to iterate any kind of content by just using the ForEach statement..

