1
00:00:00,940 --> 00:00:04,170
Let's jump into the PowerShell console and explore some of those

2
00:00:04,170 --> 00:00:07,540
concepts we just covered in the last clip.

3
00:00:07,540 --> 00:00:11,280
First up, if you recall in the formatting workflow diagram,

4
00:00:11,280 --> 00:00:13,370
if we have an object that's a string,

5
00:00:13,370 --> 00:00:17,140
Out‑Default will just pipe it straight to Out‑Host.

6
00:00:17,140 --> 00:00:21,540
Let me create a string here with a variable of $mystring,

7
00:00:21,540 --> 00:00:24,860
and I'll just put the value as This is a string of text.

8
00:00:24,860 --> 00:00:26,850
Nothing too exciting.

9
00:00:26,850 --> 00:00:33,380
If I grab $mystring and pipe that to Get‑Member and just scroll up there,

10
00:00:33,380 --> 00:00:38,440
we can see the type of object here is indeed System.String.

11
00:00:38,440 --> 00:00:41,020
If I just run $mystring,

12
00:00:41,020 --> 00:00:43,970
we get the value of that string output back on the screen.

13
00:00:43,970 --> 00:00:48,840
Behind the scenes here, that is going to Out‑Default and Out‑Host.

14
00:00:48,840 --> 00:00:51,040
If I pipe it to one of the format commands,

15
00:00:51,040 --> 00:00:55,190
like Format‑Table, normally you'd expect to see a table as the output,

16
00:00:55,190 --> 00:00:58,860
but because this is just a string and not an object with properties,

17
00:00:58,860 --> 00:01:01,040
there's nothing to format in a table.

18
00:01:01,040 --> 00:01:05,140
And just to prove that point further, same deal with Format‑List.

19
00:01:05,140 --> 00:01:09,940
We'll look more at Format‑List and Format‑Table in the upcoming clips.

20
00:01:09,940 --> 00:01:11,240
Now remember I mentioned,

21
00:01:11,240 --> 00:01:15,080
every time you run a command it gets output to Out‑Default,

22
00:01:15,080 --> 00:01:19,340
which will then, by default, pipe it to Out‑Host.

23
00:01:19,340 --> 00:01:22,030
Let's run a command like Get‑NetAdapter,

24
00:01:22,030 --> 00:01:24,650
and this will list the network adapters on the machine.

25
00:01:24,650 --> 00:01:28,940
By default, this type of object gets formatted as a table.

26
00:01:28,940 --> 00:01:33,450
You can pipe that to Out‑Default, and you get exactly the same thing,

27
00:01:33,450 --> 00:01:37,150
because that actually happened anyway when we ran it the first time,

28
00:01:37,150 --> 00:01:38,740
you just didn't see it.

29
00:01:38,740 --> 00:01:42,690
And finally, we can also explicitly pipe that to Out‑Host,

30
00:01:42,690 --> 00:01:45,760
but again, you will get exactly the same thing,

31
00:01:45,760 --> 00:01:47,590
because even though you didn't see it,

32
00:01:47,590 --> 00:01:50,280
Out‑Host was added on the end of the previous two

33
00:01:50,280 --> 00:01:53,840
commands by PowerShell automatically.

34
00:01:53,840 --> 00:01:56,170
Let's take a look at how the formatting data works

35
00:01:56,170 --> 00:01:58,240
that I mentioned in the last clip.

36
00:01:58,240 --> 00:02:02,280
If I run Get‑Process, the default formatting is a table.

37
00:02:02,280 --> 00:02:05,060
If I scroll up, you can see the table headers there,

38
00:02:05,060 --> 00:02:07,060
and take note of these table headers,

39
00:02:07,060 --> 00:02:11,040
especially things like this K and M in the brackets there.

40
00:02:11,040 --> 00:02:14,840
If I run Get‑Process again and pipe it to Get‑Member,

41
00:02:14,840 --> 00:02:16,880
and I'm just going to pipe that to the more command so

42
00:02:16,880 --> 00:02:18,390
it doesn't escape off the screen,

43
00:02:18,390 --> 00:02:24,740
here we can see the type name for this object is System.Diagnostics.Process.

44
00:02:24,740 --> 00:02:27,870
I'm going to highlight that and copy it to my clipboard using

45
00:02:27,870 --> 00:02:31,540
Ctrl+C because I want to use that again in a moment.

46
00:02:31,540 --> 00:02:36,880
One of the cmdlets I briefly introduced in the last clip was Get‑FormatData,

47
00:02:36,880 --> 00:02:39,720
which allows us to explore some of the default formatting

48
00:02:39,720 --> 00:02:42,740
rules PowerShell has in its system.

49
00:02:42,740 --> 00:02:47,840
Let's explore that by running Get‑FormatData with no parameters at all.

50
00:02:47,840 --> 00:02:51,320
The list we get back is of all the different types of objects that

51
00:02:51,320 --> 00:02:55,040
PowerShell has some specific formatting instructions for.

52
00:02:55,040 --> 00:02:58,660
We can narrow this down by specifying the object type.

53
00:02:58,660 --> 00:03:03,940
So, I'll run Get‑FormatData again, and this time use the TypeName parameter,

54
00:03:03,940 --> 00:03:07,230
and then press Ctrl+V to paste in the process object

55
00:03:07,230 --> 00:03:09,340
that I copied a few moments ago.

56
00:03:09,340 --> 00:03:13,000
One of the tips that you'll see if you dig into the help for Get‑FormatData

57
00:03:13,000 --> 00:03:17,500
is to always include the PowerShell version parameter with the value as 5.1

58
00:03:17,500 --> 00:03:21,640
when using a local invocation of Get‑FormatData.

59
00:03:21,640 --> 00:03:24,830
The result we get back here indicates there are several different

60
00:03:24,830 --> 00:03:30,040
FormatViewDefinitions for the System.Diagnostics.Process object type.

61
00:03:30,040 --> 00:03:32,240
Let me up arrow to get that back,

62
00:03:32,240 --> 00:03:35,390
and then I'll press the Home button to go to the start of the command,

63
00:03:35,390 --> 00:03:38,880
and I'm going to put that in a variable named $formatdata.

64
00:03:38,880 --> 00:03:44,990
We can then run $formatdata.FormatViewDefinition to

65
00:03:44,990 --> 00:03:46,970
look at the values of that property,

66
00:03:46,970 --> 00:03:49,800
and we can see the four different ViewDefinitions,

67
00:03:49,800 --> 00:03:53,050
three of which are specified as TableControl and one of

68
00:03:53,050 --> 00:03:55,940
which is specified as WideControl.

69
00:03:55,940 --> 00:03:59,210
If I run that again and then get the first object

70
00:03:59,210 --> 00:04:01,340
and look at the control property,

71
00:04:01,340 --> 00:04:04,960
this is now getting down into the nitty‑gritty of that first definition,

72
00:04:04,960 --> 00:04:07,910
and we can start to see properties like headers,

73
00:04:07,910 --> 00:04:10,640
rows, so on, and so forth.

74
00:04:10,640 --> 00:04:13,580
If I up arrow to get that last command back and then

75
00:04:13,580 --> 00:04:15,340
check out the Headers property,

76
00:04:15,340 --> 00:04:18,000
you'll notice we're now listing the values of the headers that

77
00:04:18,000 --> 00:04:20,360
are going to be in the formatted output,

78
00:04:20,360 --> 00:04:25,710
as well as some of the format instructions like how many pixels wide will it be,

79
00:04:25,710 --> 00:04:28,740
and what's the alignment going to be, that sort of thing.

80
00:04:28,740 --> 00:04:32,640
Remember when we ran Get‑Process a minute ago, these were the headings for

81
00:04:32,640 --> 00:04:36,240
the table columns that were displayed on our screen.

82
00:04:36,240 --> 00:04:38,870
We could keep digging around in the console here,

83
00:04:38,870 --> 00:04:42,360
but all of this information can be exported to XML,

84
00:04:42,360 --> 00:04:44,900
which, and I thought I'd never say this,

85
00:04:44,900 --> 00:04:47,140
can be a little bit easier to read.

86
00:04:47,140 --> 00:04:52,840
Let me just create a new folder in C:\Docs by running New‑Item,

87
00:04:52,840 --> 00:04:55,840
and I'll set the type to a Directory.

88
00:04:55,840 --> 00:05:00,620
Let's again run Get‑FormatData, and we'll specify the TypeName parameter,

89
00:05:00,620 --> 00:05:03,510
and I'll paste the value that's still on my clipboard,

90
00:05:03,510 --> 00:05:06,260
and we'll specify the PowerShell version.

91
00:05:06,260 --> 00:05:10,840
From there, we can pipe that across to Export‑FormatData,

92
00:05:10,840 --> 00:05:15,640
specify the path as C:\Docs/process.xml.

93
00:05:15,640 --> 00:05:19,210
Now you are meant to export these as ps1 XMLs,

94
00:05:19,210 --> 00:05:23,240
but plain old XML will work fine just for our purpose here.

95
00:05:23,240 --> 00:05:25,890
And then there's a parameter to include the script block,

96
00:05:25,890 --> 00:05:27,780
which it isn't done by default,

97
00:05:27,780 --> 00:05:30,930
but I know this view uses them and I want to show you those,

98
00:05:30,930 --> 00:05:32,570
so I'm going to include it.

99
00:05:32,570 --> 00:05:37,540
Okay, awesome, let's crack open that file in Notepad,

100
00:05:37,540 --> 00:05:40,440
and let me just make that full screen,

101
00:05:40,440 --> 00:05:46,740
and I'll bump up the font size here so we can actually see this as well.

102
00:05:46,740 --> 00:05:51,760
What we're looking at here are the formatting ViewDefinitions in XML format

103
00:05:51,760 --> 00:05:56,040
that PowerShell will use for the process object type.

104
00:05:56,040 --> 00:05:58,590
So we can see at the top here that this first view

105
00:05:58,590 --> 00:06:01,140
that's defined is named process.

106
00:06:01,140 --> 00:06:04,280
And then it starts defining the formatting information for how

107
00:06:04,280 --> 00:06:07,540
that table should be constructed by PowerShell.

108
00:06:07,540 --> 00:06:09,190
If I scroll down a little bit,

109
00:06:09,190 --> 00:06:13,600
we can see here that there are script blocks included for the table columns.

110
00:06:13,600 --> 00:06:16,850
So on the fly, when PowerShell is formatting this data,

111
00:06:16,850 --> 00:06:21,490
it is also taking these raw properties of each object like PM and

112
00:06:21,490 --> 00:06:26,860
WS, and then dividing them by 1MB to manipulate the output we end

113
00:06:26,860 --> 00:06:29,240
up getting shown by PowerShell.

114
00:06:29,240 --> 00:06:33,070
So the person that created this view is going to a lot of effort for us to

115
00:06:33,070 --> 00:06:37,840
modify and present the data to make it nicer to read by default.

116
00:06:37,840 --> 00:06:40,670
This does also highlight something to be aware of.

117
00:06:40,670 --> 00:06:42,580
Sometimes when you run a command,

118
00:06:42,580 --> 00:06:45,490
you'll see a heading in the output that isn't actually

119
00:06:45,490 --> 00:06:47,650
the name of an underlying property,

120
00:06:47,650 --> 00:06:51,240
and that's because the view is formatting it differently.

121
00:06:51,240 --> 00:06:53,310
Occasionally, you'll try and do something with that

122
00:06:53,310 --> 00:06:56,340
sub‑property with the name that you see in the output,

123
00:06:56,340 --> 00:06:59,940
only to find that it's not actually the name of the underlying property.

124
00:06:59,940 --> 00:07:02,150
So if you ever get into that situation,

125
00:07:02,150 --> 00:07:04,270
make sure you pipe the command to Get‑Member,

126
00:07:04,270 --> 00:07:08,440
and that will show you the actual property names of the object.

127
00:07:08,440 --> 00:07:09,170
Okay,

128
00:07:09,170 --> 00:07:12,850
now, let's jump back to the PowerShell console and take a look

129
00:07:12,850 --> 00:07:15,760
at an example of what PowerShell does when there are no

130
00:07:15,760 --> 00:07:18,240
formatting rules for an object type.

131
00:07:18,240 --> 00:07:23,200
I'm going to run Test‑NetConnection, and I'm going to specify the ComputerName,

132
00:07:23,200 --> 00:07:28,640
which is really just a target, as www.google.com.

133
00:07:28,640 --> 00:07:34,640
And I'm going to test if I can connect to google.com on the common port of HTTP.

134
00:07:34,640 --> 00:07:38,640
The result comes back okay there, and I can see that that's come back as a list,

135
00:07:38,640 --> 00:07:42,040
and we're looking at six properties in total.

136
00:07:42,040 --> 00:07:45,940
Let's pipe that across to Get‑Member, and if I scroll up a touch,

137
00:07:45,940 --> 00:07:49,640
the TypeName is Test‑NetConnectionResult.

138
00:07:49,640 --> 00:07:50,840
As far as I'm aware,

139
00:07:50,840 --> 00:07:54,060
there's no formatting rule for that type, so this is where

140
00:07:54,060 --> 00:07:57,430
if there are four properties or less, PowerShell will do a table,

141
00:07:57,430 --> 00:08:01,140
but if there are five or more, it will do a list.

142
00:08:01,140 --> 00:08:03,880
Let's test out that theory by running the command again,

143
00:08:03,880 --> 00:08:06,700
but this time I'm going to pipe it to Select‑Object

144
00:08:06,700 --> 00:08:09,040
to select specific properties.

145
00:08:09,040 --> 00:08:12,340
I'll pick the ComputerName, the RemoteAddress,

146
00:08:12,340 --> 00:08:15,840
the RemotePort and the SourceAddress.

147
00:08:15,840 --> 00:08:18,080
What do you think is going to happen here?

148
00:08:18,080 --> 00:08:23,740
If you think that we'll get a table as a result, you are absolutely correct.

149
00:08:23,740 --> 00:08:26,340
Let me up arrow to get that last command back,

150
00:08:26,340 --> 00:08:28,450
and then I'm going to add a fifth property.

151
00:08:28,450 --> 00:08:30,940
I'll choose PingSucceeded.

152
00:08:30,940 --> 00:08:32,970
What do you think is going to happen now?

153
00:08:32,970 --> 00:08:37,560
If you thought it will format as a list, then again, you are spot on.

154
00:08:37,560 --> 00:08:40,510
We can override the formatting system as well.

155
00:08:40,510 --> 00:08:40,790
So,

156
00:08:40,790 --> 00:08:43,520
if I press the up arrow again to get that command back

157
00:08:43,520 --> 00:08:47,540
that's selecting five properties, so by default this is a list,

158
00:08:47,540 --> 00:08:50,820
I can explicitly pipe that to Format‑Table to tell

159
00:08:50,820 --> 00:08:53,210
PowerShell that I definitely want a table,

160
00:08:53,210 --> 00:09:00,000
and because I've overridden the formatting system, I'll get the table as an output, which is exactly what I wanted.

