1
00:00:00,140 --> 00:00:03,230
Another option that we have for enumeration is what's

2
00:00:03,230 --> 00:00:06,640
referred to as the foreach numerator.

3
00:00:06,640 --> 00:00:10,260
So the first question would be what is a foreach enumerator?

4
00:00:10,260 --> 00:00:10,480
Well,

5
00:00:10,480 --> 00:00:13,500
the foreach statement is a scripting language construct for

6
00:00:13,500 --> 00:00:15,840
iterating through collections of items.

7
00:00:15,840 --> 00:00:20,550
You can then execute operations against each item that you found,

8
00:00:20,550 --> 00:00:23,820
and then you can process each item within the collection

9
00:00:23,820 --> 00:00:28,340
unless obviously you change the execution path.

10
00:00:28,340 --> 00:00:33,940
So what is in a for loop statement or a foreach loop statement?

11
00:00:33,940 --> 00:00:37,990
Well, the construct for each for loop statement is straightforward.

12
00:00:37,990 --> 00:00:43,930
It basically says foreach item in a collection, perform the specified task.

13
00:00:43,930 --> 00:00:47,040
So of course, the first thing in there is an item.

14
00:00:47,040 --> 00:00:50,260
So this is the variable that contains the current

15
00:00:50,260 --> 00:00:53,040
item that we're iterating through.

16
00:00:53,040 --> 00:00:56,210
The second is the collection that we're getting the items from.

17
00:00:56,210 --> 00:00:59,910
So you're going to say foreach item in the collection,

18
00:00:59,910 --> 00:01:02,280
the collection could be numbers, objects,

19
00:01:02,280 --> 00:01:03,740
files, etc.

20
00:01:03,740 --> 00:01:05,790
And then, of course, we have a script block,

21
00:01:05,790 --> 00:01:11,440
which is what happens when we've retrieved a specific item.

22
00:01:11,440 --> 00:01:13,350
So what does that look like in code?

23
00:01:13,350 --> 00:01:16,360
Well, the foreach base syntax is as simple as this.

24
00:01:16,360 --> 00:01:19,240
It's foreach (Item In Collection),

25
00:01:19,240 --> 00:01:23,640
so item in the collection is in the parentheses, so that's the evaluation.

26
00:01:23,640 --> 00:01:25,570
And then, of course, we have the script block,

27
00:01:25,570 --> 00:01:29,540
which is the code or the sequence of commands that need to execute.

28
00:01:29,540 --> 00:01:34,240
So if we create a variable here, which basically just has numbers 1 to 10,

29
00:01:34,240 --> 00:01:36,040
so it's effectively an array,

30
00:01:36,040 --> 00:01:41,530
I can then do a foreach statement and say foreach item, notice it's declared as

31
00:01:41,530 --> 00:01:45,590
a variable, so it's an inline variable, in the collection,

32
00:01:45,590 --> 00:01:47,790
which is the declared variable, and then,

33
00:01:47,790 --> 00:01:54,940
of course, I can write the value or perform some action on those values.

34
00:01:54,940 --> 00:01:58,010
Now, if we're looking at the syntax for this a little bit more,

35
00:01:58,010 --> 00:02:00,860
let's say we had three different types because,

36
00:02:00,860 --> 00:02:03,220
of course, when we're iterating through,

37
00:02:03,220 --> 00:02:04,800
it doesn't have to just be numbers.

38
00:02:04,800 --> 00:02:06,280
So I have collection1,

39
00:02:06,280 --> 00:02:09,310
which is numbers 1, 2, 3, 4, 5, all the way up to 10,

40
00:02:09,310 --> 00:02:11,540
collection2 is a series of letters,

41
00:02:11,540 --> 00:02:16,640
and then path equals the path to a specific file location.

42
00:02:16,640 --> 00:02:18,410
If we were to look at the number syntax,

43
00:02:18,410 --> 00:02:20,050
it would be similar to what we just looked at,

44
00:02:20,050 --> 00:02:21,930
so foreach item,

45
00:02:21,930 --> 00:02:24,930
as in each number in the collection, go and get that

46
00:02:24,930 --> 00:02:27,340
number and then write the value.

47
00:02:27,340 --> 00:02:30,320
If I was looking at the letters, the same principle would apply.

48
00:02:30,320 --> 00:02:33,110
So foreach item, the syntax doesn't change.

49
00:02:33,110 --> 00:02:35,950
You don't reference a different type of object

50
00:02:35,950 --> 00:02:37,670
unless you've declared it that way.

51
00:02:37,670 --> 00:02:39,680
But it basically just iterates.

52
00:02:39,680 --> 00:02:43,040
And then, of course, this time around, if we're looking at files,

53
00:02:43,040 --> 00:02:45,000
I can combine other things together.

54
00:02:45,000 --> 00:02:46,950
So I can say foreach file,

55
00:02:46,950 --> 00:02:51,290
remember that file and item are just arbitrary names for the variables.

56
00:02:51,290 --> 00:02:54,340
They only make sense when you call them later on.

57
00:02:54,340 --> 00:02:55,430
And then, of course, for this one,

58
00:02:55,430 --> 00:02:58,160
I want to use the path, but I can't just call path

59
00:02:58,160 --> 00:03:00,880
directly, so I have to use Get‑ChildItem,

60
00:03:00,880 --> 00:03:03,910
which is a regular out‑of‑the‑box PowerShell Command,

61
00:03:03,910 --> 00:03:06,850
which will then retrieve me an array object of all

62
00:03:06,850 --> 00:03:08,800
of the files in that directory.

63
00:03:08,800 --> 00:03:09,760
And then, at that point,

64
00:03:09,760 --> 00:03:14,990
I can just say Write‑Host Current Filename. So the foreach one is a common one.

65
00:03:14,990 --> 00:03:19,970
I use it a lot when we're iterating through various collections of data.

66
00:03:19,970 --> 00:03:22,000
It's nice and quick. It's easy to use.

