1
00:00:01,040 --> 00:00:05,380
Most of the time when using PowerShell, especially when getting started,

2
00:00:05,380 --> 00:00:07,920
you run a command and then accept the output that

3
00:00:07,920 --> 00:00:10,540
you're shown by default on the screen.

4
00:00:10,540 --> 00:00:13,350
But by understanding the PowerShell formatting system,

5
00:00:13,350 --> 00:00:16,870
you can use it to your advantage to get data in exactly the

6
00:00:16,870 --> 00:00:20,140
format you want and need when you need it.

7
00:00:20,140 --> 00:00:24,870
In PowerShell, we're familiar with running a command, let' say Get‑Process.

8
00:00:24,870 --> 00:00:26,170
And when that executes,

9
00:00:26,170 --> 00:00:28,620
we get some output on the screen in the PowerShell

10
00:00:28,620 --> 00:00:31,490
console in a human‑readable format.

11
00:00:31,490 --> 00:00:32,750
That's great and all,

12
00:00:32,750 --> 00:00:37,110
but what is this magic happening in the middle here that takes our input,

13
00:00:37,110 --> 00:00:38,720
in this instance Get‑Process,

14
00:00:38,720 --> 00:00:42,140
and then displays it for us in a way we can understand?

15
00:00:42,140 --> 00:00:44,640
Put simply, that's no magic at all.

16
00:00:44,640 --> 00:00:47,940
That is PowerShell's format system at work.

17
00:00:47,940 --> 00:00:50,220
Let's spend a few minutes breaking this down to

18
00:00:50,220 --> 00:00:53,740
understand the format system in more detail.

19
00:00:53,740 --> 00:00:56,020
There's a secret I need to let you in on.

20
00:00:56,020 --> 00:00:58,160
When you run a PowerShell command,

21
00:00:58,160 --> 00:01:01,330
let's assume Get‑Process again for this example,

22
00:01:01,330 --> 00:01:04,070
the objects that are returned by the command are

23
00:01:04,070 --> 00:01:06,200
passed down the PowerShell pipeline,

24
00:01:06,200 --> 00:01:10,140
even if you aren't explicitly piping them to another command.

25
00:01:10,140 --> 00:01:14,630
What you don't see and, until now, have probably been unaware of,

26
00:01:14,630 --> 00:01:16,500
is that every time you run a command,

27
00:01:16,500 --> 00:01:21,140
it is always piped down to a cmdlet named Out‑Default.

28
00:01:21,140 --> 00:01:24,110
It's always there because PowerShell adds it at the

29
00:01:24,110 --> 00:01:26,640
end of the pipeline automatically.

30
00:01:26,640 --> 00:01:30,370
It's the job of Out‑Default to decide how to format the data

31
00:01:30,370 --> 00:01:34,010
that has been piped to it and output the object stream of

32
00:01:34,010 --> 00:01:36,540
what is currently in the pipeline.

33
00:01:36,540 --> 00:01:39,610
There's a couple of rules that Out‑Default follows.

34
00:01:39,610 --> 00:01:44,840
First off, it checks to see if the object coming down the pipeline is a string.

35
00:01:44,840 --> 00:01:49,800
If it is, Out‑Default pipes it directly to another cmdlet named Out‑Host,

36
00:01:49,800 --> 00:01:55,440
which calls the appropriate APIs provided by the host to display the data.

37
00:01:55,440 --> 00:01:58,860
What I mean by the host is the application that you're using

38
00:01:58,860 --> 00:02:01,340
to interact with the PowerShell system.

39
00:02:01,340 --> 00:02:03,640
Often, this is the standard PowerShell console,

40
00:02:03,640 --> 00:02:05,540
which is what I'll be using in my demos.

41
00:02:05,540 --> 00:02:09,990
But it might also be tools like Visual Studio Code with the built‑in terminal,

42
00:02:09,990 --> 00:02:14,940
or the older ISE from Microsoft was another example of a host.

43
00:02:14,940 --> 00:02:18,070
Out‑Host is another cmdlet that you don't need to

44
00:02:18,070 --> 00:02:20,090
explicitly call in your command.

45
00:02:20,090 --> 00:02:21,860
It happens automatically.

46
00:02:21,860 --> 00:02:26,340
Now if the object coming down the pipeline isn't just a string,

47
00:02:26,340 --> 00:02:29,380
this is where things can get a little bit more involved.

48
00:02:29,380 --> 00:02:34,940
Out‑Default inspects the object being piped to it to determine what to do.

49
00:02:34,940 --> 00:02:37,550
First off, it will look at the object type,

50
00:02:37,550 --> 00:02:41,890
and it will determine whether there's a registered view or a formatter for this

51
00:02:41,890 --> 00:02:46,240
object type that contains specific formatting instructions.

52
00:02:46,240 --> 00:02:48,590
We'll circle back to the registered views in a moment.

53
00:02:48,590 --> 00:02:50,240
Just stick with me here.

54
00:02:50,240 --> 00:02:52,260
If there is a registered view,

55
00:02:52,260 --> 00:02:55,460
this will contain information on how to format the data,

56
00:02:55,460 --> 00:03:00,170
such as whether the data output should default to being a table or a list

57
00:03:00,170 --> 00:03:03,010
and which properties of the object should be displayed.

58
00:03:03,010 --> 00:03:07,830
And if it's a table, how wide the columns should be, so on and so forth.

59
00:03:07,830 --> 00:03:12,440
That then gets sent to Out‑Host to be displayed back to you.

60
00:03:12,440 --> 00:03:15,610
If there's not a registered view for the object type,

61
00:03:15,610 --> 00:03:19,660
then Out‑Default looks at the first object coming down the pipeline

62
00:03:19,660 --> 00:03:22,350
to determine how many properties these object has,

63
00:03:22,350 --> 00:03:25,840
and then it follows a relatively simple rule.

64
00:03:25,840 --> 00:03:27,970
If the object has five or more properties,

65
00:03:27,970 --> 00:03:32,580
it sends the entire object stream to a cmdlet named Format‑List,

66
00:03:32,580 --> 00:03:36,640
which will form at the data as a list and output it to the host.

67
00:03:36,640 --> 00:03:38,870
If it has four or fewer properties,

68
00:03:38,870 --> 00:03:44,060
it will send the entire stream to a cmdlet named Format‑Table where the data is

69
00:03:44,060 --> 00:03:47,670
formatted into a table on the assumption that there is enough screen real

70
00:03:47,670 --> 00:03:51,440
estate for you to view all four or fewer properties.

71
00:03:51,440 --> 00:03:54,940
This default behavior can be overridden by you by explicitly

72
00:03:54,940 --> 00:03:58,200
piping to a format command if you choose to do so,

73
00:03:58,200 --> 00:04:01,340
and that's something we'll explore in the next clip.

74
00:04:01,340 --> 00:04:04,730
I promised I'd swing back around and talk about these registered views,

75
00:04:04,730 --> 00:04:07,140
so let's spend a moment or two on them.

76
00:04:07,140 --> 00:04:11,240
Microsoft ships PowerShell with a number of registered views.

77
00:04:11,240 --> 00:04:14,530
You can create your own views for PowerShell to use and then load

78
00:04:14,530 --> 00:04:17,790
them in with a cmdlet named Update‑Format‑Data,

79
00:04:17,790 --> 00:04:20,460
but that's well outside of the scope of this course.

80
00:04:20,460 --> 00:04:22,450
In Windows PowerShell,

81
00:04:22,450 --> 00:04:27,630
the views were defined in XML files that were stored in the PSHome directory,

82
00:04:27,630 --> 00:04:30,140
which is where PowerShell was installed.

83
00:04:30,140 --> 00:04:32,420
You could find them by running a command such as

84
00:04:32,420 --> 00:04:34,640
what's on screen with Get‑ChildItem,

85
00:04:34,640 --> 00:04:39,440
and these contain formatting instructions for different object types.

86
00:04:39,440 --> 00:04:41,520
Now with PowerShell 7,

87
00:04:41,520 --> 00:04:45,380
the default views for objects are defined in the PowerShell source code.

88
00:04:45,380 --> 00:04:47,440
They aren't external files.

89
00:04:47,440 --> 00:04:52,210
The format.ps1xml files from PowerShell 5.1 and earlier

90
00:04:52,210 --> 00:04:55,940
don't exist in PowerShell 6 and onwards.

91
00:04:55,940 --> 00:05:00,610
But what we can do to see the formatting rules is leverage a cmdlet named

92
00:05:00,610 --> 00:05:05,490
Get‑FormatData to get information from PowerShell as to how it is going

93
00:05:05,490 --> 00:05:08,640
to format output based on the object type.

94
00:05:08,640 --> 00:05:11,110
I've extracted a couple of key lines from the help

95
00:05:11,110 --> 00:05:12,870
file to bring to your attention.

96
00:05:12,870 --> 00:05:18,340
So Get‑FormatData gets the formatting data in the current session.

97
00:05:18,340 --> 00:05:21,250
You can use this cmdlet to examine the formatting data,

98
00:05:21,250 --> 00:05:23,020
and we'll see that in the next clip.

99
00:05:23,020 --> 00:05:27,370
Then you can use Export‑FormatData to serialize the objects,

100
00:05:27,370 --> 00:05:33,040
convert them to XML, and save them in format.ps1xml files.

101
00:05:33,040 --> 00:05:38,040
I'll also show you how to export the data out to XML in the next clip.

102
00:05:38,040 --> 00:05:42,970
Later in this module, we're going to explore three formatting commands in detail,

103
00:05:42,970 --> 00:05:46,240
and I've mentioned two of them already in this clip.

104
00:05:46,240 --> 00:05:50,280
The first is Format‑Table, which is pretty self‑explanatory.

105
00:05:50,280 --> 00:05:53,680
It will format the selected data to a table.

106
00:05:53,680 --> 00:05:57,570
The second is Format‑List, which is also self‑explanatory.

107
00:05:57,570 --> 00:06:00,740
The data will get formatted into a list for us.

108
00:06:00,740 --> 00:06:05,900
The third is Format‑Wide, which formats the data in a table‑like structure,

109
00:06:05,900 --> 00:06:10,540
but it will only display one property for each object being formatted.

110
00:06:10,540 --> 00:06:13,600
The format commands do share some commonalities between them,

111
00:06:13,600 --> 00:06:16,140
which I want to briefly touch on here.

112
00:06:16,140 --> 00:06:19,750
You can tell PowerShell to autosize the results. By default,

113
00:06:19,750 --> 00:06:23,060
PowerShell does a pretty good job of determining how to display the

114
00:06:23,060 --> 00:06:26,040
data based on your console configuration.

115
00:06:26,040 --> 00:06:29,190
But there may be times where forcing it to autosize makes

116
00:06:29,190 --> 00:06:31,540
it display just that little bit nicer.

117
00:06:31,540 --> 00:06:34,880
You can group results when formatting by using a parameter

118
00:06:34,880 --> 00:06:38,640
named GroupBy, which allows you to group the results from a

119
00:06:38,640 --> 00:06:41,740
command based on a particular property.

120
00:06:41,740 --> 00:06:44,380
If you do this, you should sort the data first,

121
00:06:44,380 --> 00:06:47,790
but I'll show you that in the demos. All of the format

122
00:06:47,790 --> 00:06:50,680
commands allow you to specify alternate views,

123
00:06:50,680 --> 00:06:53,840
which is not really a common topic. But if someone has gone to the

124
00:06:53,840 --> 00:06:56,230
effort of creating a view for the data type,

125
00:06:56,230 --> 00:06:59,840
there can be some interesting use cases to explore there.

126
00:06:59,840 --> 00:07:03,880
And finally, I thought it was worth mentioning that for the most part,

127
00:07:03,880 --> 00:07:08,870
you can and probably will just pipe commands or data to format commands,

128
00:07:08,870 --> 00:07:10,870
and they will figure it out for you.

129
00:07:10,870 --> 00:07:15,240
But all of the format commands do have a parameter named InputObject,

130
00:07:15,240 --> 00:07:18,540
which allows you to specify which objects to format.

131
00:07:18,540 --> 00:07:22,710
So, for example, you might have the results from a command stored in a variable.

132
00:07:22,710 --> 00:07:25,000
And then you can just specify the variable in the

133
00:07:25,000 --> 00:07:28,540
InputObject parameter of a format command.

134
00:07:28,540 --> 00:07:31,320
It's important to pause here and reflect that the

135
00:07:31,320 --> 00:07:34,240
format commands just format the data.

136
00:07:34,240 --> 00:07:38,430
They are not responsible for displaying it or doing anything else with it,

137
00:07:38,430 --> 00:07:41,240
like sending it out to a file or to the host.

138
00:07:41,240 --> 00:07:45,630
The Out cmdlets are always used to do something with formatted data,

139
00:07:45,630 --> 00:07:49,170
and we'll be exploring Out cmdlets in more detail later in this

140
00:07:49,170 --> 00:07:54,420
course. Phew, there was a fair bit to digest there. So let's jump

141
00:07:54,420 --> 00:08:00,000
into the PowerShell console and take a look at some of these concepts that we've just discussed.

