Let's jump into the PowerShell console and explore some of those concepts we just covered in the last
clip.
First up, if you recall in the formatting workflow diagram, if we have an object that's a string out
default, we'll just pipe it straight to our host.
Let me create a string here with a variable of dollar mystery and I'll just put the value as this is
a string of text.
Nothing too exciting if I grab dollar mystery and pipe that to get member and just scroll up there we
can see the type of object here is indeed system string.
If I just run dollar mystery, we get the value of that string output back on the screen behind the
scenes here that is going to out default and out host if I type it to one of the format commands like
format table, normally you'd expect to see a table as the output.
But because this is just a string and not an object with properties, there's nothing to format in a
table.
And just to prove that point further, same deal with format list.
We'll look more at format list and format table in the upcoming clips.
Now remember I mentioned every time you run a command, it gets output to our default, which will then
by default pipe it to our host.
Let's run a command like get net adapter and this will list the network adapters on the machine by default.
This type of object gets formatted as a table.
You can pipe that to our default and you get exactly the same thing because that actually happened anyway.
When we ran it the first time, you just didn't see it.
And finally, we can also explicitly pipe that to our host.
But again, you will get exactly the same thing because even though you didn't see it out, host was
added on the end of the previous two commands by PowerShell automatically.
Let's take a look at how the formatting data works that I mentioned in the last clip.
If I run get process, the default formatting is a table.
If I scroll up, you can see the table headers there and take note of these table headers, especially
things like this.
Kay andme in the brackets there if I run get process again in paper to get member and I'm just going
to pipe that to the more command so it doesn't escape off the screen here we can see the type name for
this object is system diagnostics process.
I'm going to highlight that and copy it to my clipboard using control plus C because I want to use that
again in a moment.
One of the complaints I briefly introduced in the last clip was get format data, which allows us to
explore some of the default formatting rules PowerShell has in its system.
Let's explore that by running get format data with no parameters at all.
The list we get back is of all the different types of objects that PowerShell has some specific formatting
instructions for.
We can narrow this down by specifying the object type, so I'll run get format data again, and this
time use the tightening parameter and then press control plus V to paste in the process object that
I copied a few moments ago.
One of the tips that you'll see if you dig into the help forget format data is to always include the
PowerShell version parameter with the value as 5.1 when using a local invocation of get format data.
The result we get back here indicates there are several different format view definitions for the system
diagnostics process, object type.
Let me up arrow to get that back and then I'll press the home button to go to the start of the command
and I'm going to put that in a variable named format data.
We can then run format data dot format view definition to look at the values of that property.
And we can see the four different view definitions, three of which are specified as table control and
one of which is specified as white control.
If I run that again and then get the first object and look at the control property, this is now getting
down into the nitty gritty of that first definition and we can start to see properties like headers
rose, so on and so forth.
If I up arrow to get that last command back and then check out the Heather's property, you'll notice
we're now listing the values of the headers that are going to be in the formatted output, as well as
some of the format instructions like how many pixels wide will it be and what's the alignment going
to be?
That sort of thing.
Remember when we ran Get Process a minute ago?
These were the headings for the table columns that were displayed on our screen.
We could keep digging around in the console here, but all of this information can be exported to Excel,
which and I thought I'd never say this can be a little bit easier to read.
Let me just create a new folder in C Colon Backslash docs by running new item and I'll set the type
to a directory.
Let's again run get format data and we'll specify the type named parameter and I'll paste the value
that's still on my clipboard and we'll specify the PowerShell version.
From there, we can pipe that across to export format data, specify the path a c colon backslash docs
forward slash process dot xml.
Now you are meant to export these as one examples, but plain old XML will work fine just for our purpose
here.
And then there's a parameter to include the script block, which it isn't done by default.
But I know this view uses them and I want to show you those, so I'm going to include it.
Okay, awesome.
Let's crack open that file in notepad and let me just make that full screen and I'll bump up the font
size here so we can actually see this as well.
What we're looking at here are the formatting you definitions in XML format that PowerShell will use
for the process object type.
So we can see at the top here that this first view that's defined is named process, and then it starts
defining the formatting information for how that table should be constructed by PowerShell.
If I scroll down a little bit, we can see here that there are script blocks included for the table
columns.
So on the fly, when PowerShell is formatting this data, it is also taking these raw properties of
each object like PM and WC and then dividing them by one megabyte to manipulate the output.
We end up getting shown by PowerShell.
So the person that created this view is going to a lot of effort for us to modify and present the data
to make it nicer to read by default.
This does also highlight something to be aware of.
Sometimes when you run a command, you'll see a heading in the output that isn't actually the name of
an underlying property, and that's because the view is formatting it differently.
Occasionally you'll try and do something with that sub property with the name that you see in the output,
only to find that it's not actually the name of the underlying property.
So if you ever get into that situation, make sure you pipe the command to get member and that will
show you the actual property names of the object.
Okay.
Now let's jump back to the PowerShell console and take a look at an example of what PowerShell does
when there are no formatting rules for an object type.
I'm going to run Testnet connection and I'm going to specify the computer name, which is really just
a target as w w w dot google.com.
And I'm going to test if I can connect to Google.com on the common port of HTTP.
The result comes back.
Okay there and I can see that that's come back as a list and we're looking at six properties in total.
Let's pipe that across to get member and if I scroll up a touch, the type name is Testnet connection
result.
As far as I'm aware, there's no formatting rule for that type.
So this is where if there are four properties or less PowerShell, we'll do a table.
But if there are five or more, it will do a list.
Let's test out that theory by running the command again.
But this time I'm going to pipe it to select object to select specific properties.
I'll pick the computer name.
The remote address, the remote port and the source address.
What do you think is going to happen here?
If you think that we'll get a table as a result, you are absolutely correct.
Let me up arrow to get that last command back and then I'm going to add a fifth property.
I'll choose Pink succeeded.
What do you think is going to happen now?
If you thought it will format as a list, then again you are spot on.
We can override the formatting system as well.
So if I press the up arrow again to get that command back, that's selecting five properties.
So by default, this is a list I can explicitly pipe that to format table to tell PowerShell that I
definitely want a table.
And because I've overridden the formatting system, I'll get the table as an output, which is exactly
what I wanted.