WEBVTT

00:03.050 --> 00:07.010
The four reach construct has the same purpose as the for each object complete.

00:07.880 --> 00:13.010
That purpose is to enumerate through a collection of objects so that you can work with one object at

00:13.010 --> 00:13.520
a time.

00:14.360 --> 00:18.430
The construct has a different syntax than the complete note.

00:18.440 --> 00:23.690
The for each object complete has an alias for each that is the same as the for each construct keyword.

00:24.560 --> 00:27.500
However, the complete and the construct are not the same.

00:28.310 --> 00:33.650
Windows PowerShell decides whether you are using the LED or the construct based on its position on the

00:33.650 --> 00:34.400
command line.

00:35.210 --> 00:40.310
Although both the construct and the complete have the same purpose, they do not use the same syntax.

00:41.180 --> 00:49.190
The construct looks as follows The computer names equals get content names dot text for each dollar

00:49.190 --> 00:50.660
name and dollar computer names.

00:51.530 --> 00:53.360
Write the current name as dollar name.

00:54.170 --> 00:58.430
In this example, names date text has one computer name per line.

00:59.240 --> 01:04.340
Those are loaded into dollar computer name as individual string objects to make dollar computer name

01:04.340 --> 01:05.000
a collection.

01:05.840 --> 01:08.870
The for each construct enumerates through those one at a time.

01:09.680 --> 01:14.750
The variable on the left side of the in keyword will contain one object at a time from the variable

01:14.750 --> 01:16.250
on the right side of the keyword.

01:17.120 --> 01:22.730
In other words, one computer name at a time will be taken out of the computer name and added into dollar

01:22.730 --> 01:23.060
name.

01:23.900 --> 01:29.270
The command or commands in the script block will execute one time for each object in the dollar computer

01:29.270 --> 01:30.050
name variable.

01:30.920 --> 01:35.480
Because most Windows PowerShell users prefer scripts that are easy to read and maintain.

01:35.690 --> 01:39.770
It is a common practice to use similar variable names in the for each construct.

01:40.670 --> 01:44.480
For example, dollar services equals get service.

01:45.380 --> 01:47.870
For each dollar service in dollar services.

01:48.810 --> 01:49.140
Right.

01:49.140 --> 01:50.520
The current service is dollar.

01:50.580 --> 01:52.050
Dollar service dot name.

01:52.920 --> 01:56.340
However, this naming practice is not a technical requirement.

01:57.230 --> 01:59.750
The following example also works correctly.

02:00.590 --> 02:02.300
Dollar X equals get service.

02:03.240 --> 02:08.160
For each dollar z in x write the current service is dollar Z.

02:09.030 --> 02:14.820
This second example is not as easy to read because the names Dollar X and Dollar Z do not provide any

02:14.820 --> 02:17.640
indication or reminder of what the variables contain.
