-
Notifications
You must be signed in to change notification settings - Fork 2
/
Add-FormatData.ps1
67 lines (52 loc) · 1.97 KB
/
Add-FormatData.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
##############################################################################
##
## Add-FormatData
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################
<#
.SYNOPSIS
Adds a table formatting definition for the specified type name.
.EXAMPLE
PS > $r = [PSCustomObject] @{
Name = "Lee";
Phone = "555-1212";
SSN = "123-12-1212"
}
PS > $r.PSTypeNames.Add("AddressRecord")
PS > Add-FormatData -TypeName AddressRecord -TableColumns Name, Phone
PS > $r
Name Phone
---- -----
Lee 555-1212
#>
param(
## The type name (or PSTypeName) that the table definition should
## apply to.
$TypeName,
## The columns to be displayed by default
[string[]] $TableColumns
)
Set-StrictMode -Version 3
## Define the columns within a table control row
$rowDefinition = New-Object Management.Automation.TableControlRow
## Create left-aligned columns for each provided column name
foreach($column in $TableColumns)
{
$rowDefinition.Columns.Add(
(New-Object Management.Automation.TableControlColumn "Left",
(New-Object Management.Automation.DisplayEntry $column,"Property")))
}
$tableControl = New-Object Management.Automation.TableControl
$tableControl.Rows.Add($rowDefinition)
## And then assign the table control to a new format view,
## which we then add to an extended type definition. Define this view for the
## supplied custom type name.
$formatViewDefinition = New-Object Management.Automation.FormatViewDefinition "TableView",$tableControl
$extendedTypeDefinition = New-Object Management.Automation.ExtendedTypeDefinition $TypeName
$extendedTypeDefinition.FormatViewDefinition.Add($formatViewDefinition)
## Add the definition to the session, and refresh the format data
[Runspace]::DefaultRunspace.InitialSessionState.Formats.Add($extendedTypeDefinition)
Update-FormatData