Skip to content

Instantly share code, notes, and snippets.

@adamdriscoll
Last active April 10, 2018 18:36
Show Gist options
  • Save adamdriscoll/0c44b27731c1857827a55832957390a2 to your computer and use it in GitHub Desktop.
Save adamdriscoll/0c44b27731c1857827a55832957390a2 to your computer and use it in GitHub Desktop.
Formatting in Out-UDGridView
$FormatData = Get-FormatData -TypeName (Get-Process | Get-Member).TypeName
if ($FormatData -ne $null) {
$TableControl = $FormatData.FormatViewDefinition.Control | Where-Object { $_ -is [System.Management.Automation.TableControl] }
if ($TableControl -ne $null) {
$Row = $TableControl.Rows | Select-Object -First 1
$i = 0
foreach($Header in $TableControl.Headers) {
if ([String]::IsNullOrEmpty($Header.Label)) {
$Headers += $Row.Columns[$i].DisplayEntry.Value
}
else {
$Headers += $Header.Label
}
$i++
}
}
foreach($Item in $Script:Items) {
$FormattedItem = @{}
$c = 0
foreach($Column in $Row.Columns) {
if ($Column.DisplayEntry.ValueType -eq 'Property') {
$FormattedItem."p$c" = $Item.$($Column.DisplayEntry.Value)
}
elseif ($Column.DisplayEntry.ValueType -eq 'ScriptBlock') {
$FormattedItem."p$c" = [ScriptBlock]::Create($Column.DisplayEntry.Value.Replace('$_', '$args[0]')).Invoke($Item) | Select-Object -First 1
}
$c++
}
$Properties = 0..$c | ForEach-Object { "p$_" }
$FormattedItems += $FormattedItem
}
}
else {
$headers = $Script:Items | Get-Member -MemberType Property | Select-Object -ExpandProperty Name
$Properties = $Script:Items | Get-Member -MemberType Property | Select-Object -ExpandProperty Name
$FormattedItems = $Script:Items
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment