Very often you'll want to be selective in what you get
from a PowerShell command or statement.
That's where Select‑Object comes in.
There are a few ways to use this command,
including some advanced techniques that we won't get into in this course.
The first way is to select the first or last X number of objects.
So instead of getting all the files from c:\work, I'll only get the first three.
You also have an option to skip a certain number of files before selecting.
When used this way,
Select‑Object passes the original object on in the pipeline to the next command,
if there is one.
The other way to use Select‑Object, and probably the more common way, is
to select a subset of object properties. Like sorting and grouping, you
can specify any property name that you see with Get‑Member. Separate
multiple property names with commas.
In this situation,
Select‑Object creates a custom object on the fly based on the original
object and only displays the properties that you specified.
In this example, I won't see the full process object.
Instead,
I'll see a substitute object for each process that shows
the process ID, name, and working set.
Finally, there is Measure‑Object. Now the property parameter is positional,
but I'm showing it here for the sake of clarity.
Specify the property or properties that you want to
measure. Again, separate properties by commas.
As with the other commands we've looked at,
you can use any property that you discover with Get‑Member. The property
value, in this case though, must be a number. You then need to tell
Measure‑Object what type of measurement to do,
such as getting the sum of the file sizes that I'm doing here.
You can also get values like the average, maximum, and minimum values.
You'll specify these by parameters.
When you read the help and examples for Measure‑Object,
you'll also see that there is a less‑used option to
count words and lines from a text file.
Measure command writes a new type of object to the pipeline with your measurement results.