Skip to content

Instantly share code, notes, and snippets.

@gravejester
Created November 9, 2014 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravejester/8cd8e81bd16a028367a8 to your computer and use it in GitHub Desktop.
Save gravejester/8cd8e81bd16a028367a8 to your computer and use it in GitHub Desktop.
function New-FormatTableItem {
<#
.SYNOPSIS
Create new format Table Item
.DESCRIPTION
Create new format Table Item for use with Add-FormatTable
.EXAMPLE
Assuming you have a custom object with a type name of 'Custom.UserType':
@(
(New-FormatTableItem 'First Name' 'FirstName' 20),
(New-FormatTableItem 'Last Name' 'LastName' 20),
(New-FormatTableItem 'Sex' 'Sex' 10),
(New-FormatTableItem 'E-mail' 'Email' 20)
) | Add-FormatTable -TypeName 'Custom.UserType'
This will add a custom table formatting definition for the specified type name.
.NOTES
Based on a function by Lee Holmes in his excellent Windows PowerShell Cookbook (O'Reilly)
Author: Øyvind Kallstad
Date: 09.11.2014
Version: 1.0
#>
[CmdletBinding()]
param (
[Parameter()]
[string] $Label,
[Parameter(Mandatory)]
[string] $PropertyName,
[Parameter()]
[int] $Width = 20,
[Parameter()]
[System.Management.Automation.Alignment] $Alignment = 'Undefined'
)
$tableControlColumnHeader = New-Object -TypeName System.Management.Automation.TableControlColumnHeader $Label, $Width, $Alignment
$tableControlColumn = New-Object -TypeName System.Management.Automation.TableControlColumn $Alignment, (New-Object -TypeName System.Management.Automation.DisplayEntry $PropertyName, 'Property')
Write-Output ([PSCustomObject] @{
TableControlColumnHeader = $tableControlColumnHeader
TableControlColumn = $tableControlColumn
})
}
function Add-FormatTable {
<#
.SYNOPSIS
Add table formatting definition for the specified type
.EXAMPLE
Assuming you have a custom object with a type name of 'Custom.UserType':
@(
(New-FormatTableItem 'First Name' 'FirstName' 20),
(New-FormatTableItem 'Last Name' 'LastName' 20),
(New-FormatTableItem 'Sex' 'Sex' 10),
(New-FormatTableItem 'E-mail' 'Email' 20)
) | Add-FormatTable -TypeName 'Custom.UserType'
This will add a custom table formatting definition for the specified type name.
.NOTES
Based on a function by Lee Holmes in his excellent Windows PowerShell Cookbook (O'Reilly)
Author: Øyvind Kallstad
Date: 09.11.2014
Version: 1.0
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string] $TypeName,
[Parameter(ValueFromPipelineByPropertyName)]
[System.Management.Automation.TableControlColumnHeader[]] $TableControlColumnHeader,
[Parameter(ValueFromPipelineByPropertyName)]
[System.Management.Automation.TableControlColumn[]] $TableControlColumn
)
BEGIN {
$table = New-Object System.Management.Automation.TableControl
$row = New-Object System.Management.Automation.TableControlRow
}
PROCESS {
foreach($item in $TableControlColumn) {
$row.Columns.Add($item)
}
foreach($header in $TableControlColumnHeader) {
$table.Headers.Add($header)
}
}
END {
$table.Rows.Add($row)
$formatView = New-Object System.Management.Automation.FormatViewDefinition 'TableView', $table
$extendedType = New-Object System.Management.Automation.ExtendedTypeDefinition $TypeName
$extendedType.FormatViewDefinition.Add($formatView)
[Runspace]::DefaultRunspace.InitialSessionState.Formats.Add($extendedType)
Update-FormatData
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment