1
00:00:01,040 --> 00:00:04,020
Very often you'll want to be selective in what you get

2
00:00:04,020 --> 00:00:06,150
from a PowerShell command or statement.

3
00:00:06,150 --> 00:00:08,440
That's where Select‑Object comes in.

4
00:00:08,440 --> 00:00:10,820
There are a few ways to use this command,

5
00:00:10,820 --> 00:00:13,990
including some advanced techniques that we won't get into in this course.

6
00:00:13,990 --> 00:00:18,540
The first way is to select the first or last X number of objects.

7
00:00:18,540 --> 00:00:23,510
So instead of getting all the files from c:\work, I'll only get the first three.

8
00:00:23,510 --> 00:00:28,040
You also have an option to skip a certain number of files before selecting.

9
00:00:28,040 --> 00:00:29,300
When used this way,

10
00:00:29,300 --> 00:00:33,620
Select‑Object passes the original object on in the pipeline to the next command,

11
00:00:33,620 --> 00:00:34,740
if there is one.

12
00:00:34,740 --> 00:00:38,590
The other way to use Select‑Object, and probably the more common way, is

13
00:00:38,590 --> 00:00:43,410
to select a subset of object properties. Like sorting and grouping, you

14
00:00:43,410 --> 00:00:47,890
can specify any property name that you see with Get‑Member. Separate

15
00:00:47,890 --> 00:00:50,340
multiple property names with commas.

16
00:00:50,340 --> 00:00:51,700
In this situation,

17
00:00:51,700 --> 00:00:56,480
Select‑Object creates a custom object on the fly based on the original

18
00:00:56,480 --> 00:01:00,540
object and only displays the properties that you specified.

19
00:01:00,540 --> 00:01:04,750
In this example, I won't see the full process object.

20
00:01:04,750 --> 00:01:05,610
Instead,

21
00:01:05,610 --> 00:01:09,220
I'll see a substitute object for each process that shows

22
00:01:09,220 --> 00:01:12,440
the process ID, name, and working set.

23
00:01:12,440 --> 00:01:16,520
Finally, there is Measure‑Object. Now the property parameter is positional,

24
00:01:16,520 --> 00:01:19,720
but I'm showing it here for the sake of clarity.

25
00:01:19,720 --> 00:01:22,360
Specify the property or properties that you want to

26
00:01:22,360 --> 00:01:25,540
measure. Again, separate properties by commas.

27
00:01:25,540 --> 00:01:27,600
As with the other commands we've looked at,

28
00:01:27,600 --> 00:01:31,860
you can use any property that you discover with Get‑Member. The property

29
00:01:31,860 --> 00:01:36,370
value, in this case though, must be a number. You then need to tell

30
00:01:36,370 --> 00:01:39,020
Measure‑Object what type of measurement to do,

31
00:01:39,020 --> 00:01:44,240
such as getting the sum of the file sizes that I'm doing here.

32
00:01:44,240 --> 00:01:49,600
You can also get values like the average, maximum, and minimum values.

33
00:01:49,600 --> 00:01:51,940
You'll specify these by parameters.

34
00:01:51,940 --> 00:01:54,690
When you read the help and examples for Measure‑Object,

35
00:01:54,690 --> 00:01:57,420
you'll also see that there is a less‑used option to

36
00:01:57,420 --> 00:01:59,700
count words and lines from a text file.

37
00:01:59,700 --> 00:02:06,000
Measure command writes a new type of object to the pipeline with your measurement results.

