With formatting tables out of the way,
let's spend a few minutes looking at another popular format style,
lists.
Okay, as usual,
let's spend a moment checking out the help by running help Format‑List.
So this will format the output as a list of properties in
which each property appears on a new line.
Seems straightforward enough, right?
Just like writing a shopping list.
Do people still even write those?
Anyway, here in the syntax block,
there's a few similar parameters between Format‑List and Format‑Table.
And, of course, there's an alias for Format‑List.
So if I run Get‑Alias ‑Definition format‑list, we'll see the alias is simply fl.
All right, let's just run Get‑Process here.
And in case you didn't watch the previous clip,
by default, this outputs as a table.
I'm just going to rerun that and pipe it to the more command because I
want to take a quick look at these table headers here.
Take special note here to some of the names as to how they are
formatted in the table and also note that we are seeing seven
different properties in this table view.
Let's rerun Get‑Process, and this time,
we'll override the default formatting by piping it to Format‑List.
As expected, we get a list view of properties for every object.
Each object is separated by a blank line,
and each property of the object is displayed on a new line.
One thing worth noting here is take a look at the names of these properties.
So they're quite different to what we just saw when we formatted to a table.
And also, we're only seeing five properties here in the list view.
Whereas in the table view, we were saying seven.
By now, you're starting to get exposed to some of the nuances with the
formatting system and that things that are displayed to us aren't
always the raw underlying data of the objects.
Let's try grouping the results here.
Similar to Format‑Table,
we need to sort the objects first before grouping them with the format command.
So I'll run Get‑Process.
I'll pipe that across to Sort‑Object and sort on the Name property.
And from there,
we can send it to Format‑List and then use the GroupBy
parameter to group the results by the name.
And we can see that the formatting has taken care
of grouping the results for us.
So, for example, at the bottom here,
there are two objects that match the name WmiPrvSE,
but only one that matches the name of winlogon.
For the next couple of examples,
I'm going to run Get‑Process and just grab the winlogon process.
So we don't have a lot of data being output to the screen that
we have to keep scrolling up and down for.
Let me pipe that to Format‑List with no parameters,
and we get those five properties that PowerShell decides
to show us by default for a list view.
Now I've seen a lot of beginners think that if you
pipe a command to Format‑List,
that you will get shown all of the properties of that object.
That is, in fact,
incorrect because the formatting system could be choosing to
display just a number of properties back to you,
which is exactly what's happening here.
To understand what properties are on this object,
you could pipe it to Get‑Member, which is absolutely fine,
and in most cases, it's recommended.
But sometimes you might want to see the properties
with the actual values as well.
On occasion,
seeing the values right in front of you can actually make it
easier to determine what you're working with.
So a little trick we can use is to pipe it to Format‑List,
but then use the Property parameter and then use the wild card or
asterisk to tell PowerShell we want to see all properties.
In fact, because the Property parameter is positional,
you don't even need to specify it.
You could just have Format‑List * or fl *.
And as you can see,
there are many more properties than the five we were
being shown just a moment ago.
If I press the up arrow,
we can choose what properties we want the Format‑List command to output for us.
And because the Property parameter is positional,
wow, say that three times fast,
we can just put the property names without specifying the parameter,
and we'll get just those two returned in the output.
There is something you can do in PowerShell called custom properties.
And look, I don't want to bog you down with the details too much,
but I want to show you that you can make use of a custom
property in the format commands to modify the output that
you're seeing. For example,
the property name that we're looking at here for
the process name is simply Name.
What if we wanted to modify that to say Process Name instead? I'll up arrow
that to get the command back and then remove Name and replace it with an @
symbol and two curly brackets to define a custom expression. Inside of that,
I can type Name equals Process Name.
And because I'm using a space, I've put Process Name in double quotes there.
I then need to use a semicolon and then use the keyword Expression,
which is where I need to define what the value is going to be.
I'll put an equals sign and another set of
brackets. And inside of those brackets,
I'm going to use $_, which is a way of telling PowerShell that I
want the current object coming down the pipeline. And then I'm going
to specify .name to tell PowerShell that I want the value of this
custom expression to be the name property of the current process
coming down the pipeline.
If I run that, notice the value is the same as before,
but the name of the result is now Process Name
instead of just Name as it was before.
This is something you can do with any of the format commands.
The last thing I want to show here, and again this actually
applies to all of the format commands, is that so far I've been
piping commands to a format command, which then outputs it to screen.
You can use the InputObject parameter of a format command to
specify the objects that you want to be formatted.
So let's define a variable of $processes. And in
that, we'll just store Get‑Process.
Now remember that variable contains the unformatted object data of processes.
Nothing more, nothing less.
I could now run Format‑List and use the InputObject parameter and
specify the $processes variable, and PowerShell will take those
objects and format them as a list.
Functionally,
it's the same as piping it across to Format‑List, but I did want you to be aware of that capability.