1
00:00:01,040 --> 00:00:05,340
The first format command I'd like to show you is Format‑Table.

2
00:00:05,340 --> 00:00:07,440
Let's get stuck in.

3
00:00:07,440 --> 00:00:11,340
Let's check out the help by running help Format‑Table.

4
00:00:11,340 --> 00:00:13,900
So the synopsis here is pretty straightforward.

5
00:00:13,900 --> 00:00:18,680
It formats the output as a table. Sweet! There are a number

6
00:00:18,680 --> 00:00:20,960
of parameters available for Format‑Table,

7
00:00:20,960 --> 00:00:23,440
and we'll take a look at a bunch of them shortly.

8
00:00:23,440 --> 00:00:24,310
And there's, of course,

9
00:00:24,310 --> 00:00:28,840
a detailed description here if you'd like to read through that in your own time.

10
00:00:28,840 --> 00:00:31,650
Now there is an alias for Format‑Table.

11
00:00:31,650 --> 00:00:35,430
So if I run, Get‑Alias ‑Definition Format‑Table,

12
00:00:35,430 --> 00:00:40,140
we'll see the alias for this property is simply ft.

13
00:00:40,140 --> 00:00:42,660
You'll probably see that used a lot on the internet,

14
00:00:42,660 --> 00:00:45,230
and I personally use it a lot when I'm working at the console,

15
00:00:45,230 --> 00:00:47,860
as well, because it's just quicker to type.

16
00:00:47,860 --> 00:00:50,540
And no doubt you'll start to do the same.

17
00:00:50,540 --> 00:00:54,740
Let me run a command that I know doesn't output to a table by default,

18
00:00:54,740 --> 00:00:57,140
which is simply get‑date.

19
00:00:57,140 --> 00:01:01,740
This output here is actually quite customized by the PowerShell format system,

20
00:01:01,740 --> 00:01:05,180
but you can take a command, get‑date in this example,

21
00:01:05,180 --> 00:01:08,120
and then pipe it down to Format‑Table to force the

22
00:01:08,120 --> 00:01:11,110
output to be displayed in a table format.

23
00:01:11,110 --> 00:01:15,180
And it's very likely that the formatting files are telling PowerShell

24
00:01:15,180 --> 00:01:18,540
which properties to display here in this table.

25
00:01:18,540 --> 00:01:22,730
Similarly, I know the command Get‑Host will default to a list.

26
00:01:22,730 --> 00:01:27,940
Again, we can force this to a table simply by piping it to Format‑Table.

27
00:01:27,940 --> 00:01:30,710
You might notice there that the value of that last property

28
00:01:30,710 --> 00:01:32,750
is actually being truncated a little bit,

29
00:01:32,750 --> 00:01:36,190
and we get shown the three dots to let us know there is more data.

30
00:01:36,190 --> 00:01:40,940
Towards the end of this clip, I'll show you a way to stop that from happening.

31
00:01:40,940 --> 00:01:45,140
Okay, let's grab a command that does output to a table by default.

32
00:01:45,140 --> 00:01:47,840
So let's use Get‑Process.

33
00:01:47,840 --> 00:01:51,370
If I scroll up, you can see this is indeed a table,

34
00:01:51,370 --> 00:01:54,610
and there are headers at the top telling us what each column is.

35
00:01:54,610 --> 00:01:58,930
If I pipe Get‑Process to Format‑Table, well,

36
00:01:58,930 --> 00:02:01,750
I get exactly the same thing because the format system

37
00:02:01,750 --> 00:02:03,780
was already doing that by default.

38
00:02:03,780 --> 00:02:08,740
You can modify the properties being shown in the table in a few different ways.

39
00:02:08,740 --> 00:02:12,040
You could pipe Get‑Process to a select object,

40
00:02:12,040 --> 00:02:16,240
select the properties, and then pipe that down to Format‑Table.

41
00:02:16,240 --> 00:02:17,400
Alternatively,

42
00:02:17,400 --> 00:02:21,210
you could use the ‑Property parameter on Format‑Table to specify

43
00:02:21,210 --> 00:02:24,040
which properties you want to be displayed.

44
00:02:24,040 --> 00:02:28,230
Let's grab the Name, Responding, and Description properties.

45
00:02:28,230 --> 00:02:29,790
And there we go.

46
00:02:29,790 --> 00:02:31,160
I've still got the table,

47
00:02:31,160 --> 00:02:34,940
but this time I've got the specific properties that I asked for.

48
00:02:34,940 --> 00:02:37,810
I want to refer back to the help system for a moment to

49
00:02:37,810 --> 00:02:40,740
bring up help for the GroupBy parameter.

50
00:02:40,740 --> 00:02:46,340
I'll run Get‑Help Format‑Table and then specify the parameter GroupBy.

51
00:02:46,340 --> 00:02:47,880
If I scroll up here a little,

52
00:02:47,880 --> 00:02:50,440
there are a couple of things that I want to point out.

53
00:02:50,440 --> 00:02:53,320
Using this parameter will group the output in separate

54
00:02:53,320 --> 00:02:56,240
tables based on a property value.

55
00:02:56,240 --> 00:02:57,820
But, importantly,

56
00:02:57,820 --> 00:03:03,000
it says here that the GroupBy parameter expects the objects are sorted already,

57
00:03:03,000 --> 00:03:08,900
so use the Sort‑Object cmdlet before using Format‑Table to group the objects.

58
00:03:08,900 --> 00:03:09,950
Sweet!

59
00:03:09,950 --> 00:03:11,050
No worries at all.

60
00:03:11,050 --> 00:03:12,130
Let's give it a go.

61
00:03:12,130 --> 00:03:13,870
If we run Get‑Process,

62
00:03:13,870 --> 00:03:17,920
first we need to pipe that to Sort‑Object like the help told us to.

63
00:03:17,920 --> 00:03:22,540
And I want to sort the results based on the ProcessName property.

64
00:03:22,540 --> 00:03:24,540
Now that we've followed the rules and sorted,

65
00:03:24,540 --> 00:03:29,090
we can pipe that down to Format‑Table and use the GroupBy parameter

66
00:03:29,090 --> 00:03:32,240
to group the results based on the ProcessName.

67
00:03:32,240 --> 00:03:36,940
And check this out, the results are now in individual little tables,

68
00:03:36,940 --> 00:03:39,660
but they're all grouped according to their ProcessName.

69
00:03:39,660 --> 00:03:43,440
You can see here there's only one process for winlogon,

70
00:03:43,440 --> 00:03:45,340
so there's just one result there.

71
00:03:45,340 --> 00:03:49,320
But there are two processes for WmiPrvSE,

72
00:03:49,320 --> 00:03:52,040
and they're grouped together at the bottom.

73
00:03:52,040 --> 00:03:56,840
If I scroll back quickly, you'll see everything is grouped out pretty nicely.

74
00:03:56,840 --> 00:04:00,570
Another thing we can do while using GroupBy is also change the

75
00:04:00,570 --> 00:04:03,140
properties being displayed in the output.

76
00:04:03,140 --> 00:04:04,450
So, again, just like before,

77
00:04:04,450 --> 00:04:08,070
if I put in the ‑Property parameter and then grab the Name,

78
00:04:08,070 --> 00:04:10,540
Responding, and Description properties,

79
00:04:10,540 --> 00:04:13,730
it will still group the results for me based on the ProcessName,

80
00:04:13,730 --> 00:04:17,140
but it will only display the properties I've asked for.

81
00:04:17,140 --> 00:04:21,640
There are a couple of other functions of Format‑Table that I'd like to show you.

82
00:04:21,640 --> 00:04:26,120
Let's run Get‑WinEvent to grab Windows event log data from the

83
00:04:26,120 --> 00:04:29,910
application log, and let's just limit the results here to 5 so we

84
00:04:29,910 --> 00:04:32,640
don't get too much data in the console.

85
00:04:32,640 --> 00:04:37,740
Okay, so by default, we get results formatted as a table, which is cool.

86
00:04:37,740 --> 00:04:39,840
But there are a few things here.

87
00:04:39,840 --> 00:04:40,390
Firstly,

88
00:04:40,390 --> 00:04:45,140
there's a bit of dead space here between the TimeCreated and the ID properties.

89
00:04:45,140 --> 00:04:46,000
Secondly,

90
00:04:46,000 --> 00:04:48,760
the message is being cut off in all of the results because

91
00:04:48,760 --> 00:04:51,340
it's too long to show on the screen.

92
00:04:51,340 --> 00:04:55,790
Let's run that again but, this time, pipe it to Format‑Table. Now doing

93
00:04:55,790 --> 00:04:58,380
just that would result in exactly the same thing.

94
00:04:58,380 --> 00:05:03,870
So let's pop on the ‑AutoSize parameter. This time, if you look closely,

95
00:05:03,870 --> 00:05:07,520
PowerShell's done a better job of trying to make the most of the PowerShell

96
00:05:07,520 --> 00:05:12,340
console real estate to display as much information as it can.

97
00:05:12,340 --> 00:05:16,160
You can see the gap between TimeCreated and ID has been closed,

98
00:05:16,160 --> 00:05:18,600
which has allowed us to see a little bit more of the message,

99
00:05:18,600 --> 00:05:20,940
but it's still getting cut off.

100
00:05:20,940 --> 00:05:25,610
Another capability we have is to use the ‑Wrap parameter of Format‑Table,

101
00:05:25,610 --> 00:05:29,400
which will tell PowerShell to wrap the values of the properties being

102
00:05:29,400 --> 00:05:33,540
displayed to us so we can see the entire value on screen.

103
00:05:33,540 --> 00:05:37,240
A couple of other quick things to take a look at before we move on.

104
00:05:37,240 --> 00:05:40,980
Let me run, Get‑Process again and pipe it to Format‑Table,

105
00:05:40,980 --> 00:05:44,040
and there's a parameter named ‑RepeatHeader.

106
00:05:44,040 --> 00:05:45,520
If I scroll back a little bit,

107
00:05:45,520 --> 00:05:48,740
you'll see the table header is repeated every so often,

108
00:05:48,740 --> 00:05:51,360
and you might be wondering, Matt,

109
00:05:51,360 --> 00:05:56,600
why? This can be handy if you do something like pipe it to the more command.

110
00:05:56,600 --> 00:06:00,250
So those table headers will be repeated at every page scroll for

111
00:06:00,250 --> 00:06:03,800
your PowerShell console. So you don't need to keep scrolling up and

112
00:06:03,800 --> 00:06:06,540
down to see what the values represent.

113
00:06:06,540 --> 00:06:09,920
Finally, there's also a parameter named ‑HideTableHeaders,

114
00:06:09,920 --> 00:06:10,740
which,

115
00:06:10,740 --> 00:06:13,640
as you can probably guess from the name, is going to show the

116
00:06:13,640 --> 00:06:20,000
table but without the headers displayed. We'll only get the columns and the values of the properties.

