Format-Chart.ps1 – Formats output as a table with a chart column


Synopsis

The script formats output as a table with a pseudo-graphical chart column calculated for the last specified numeric property.


Examples

# Chart of process working sets
ps | Format-Chart Id, Name, WS

# Chart of file sizes in descending order
dir | sort Length -desc | Format-Chart Name, Length

Format-Chart.ps1

##
## Author   : Roman Kuzmin
## Synopsis : Formats output as a table with a chart column
## Modified : 2006.11.06
##
## -Property: properties where the last one is numeric for a chart.
## -Width: chart column width, default is 1/2 of screen buffer.
## -ForeChar: character for chart bars.
## -BackChar: character for appending bars.
## -InputObject: the objects to be formatted.
##

param
(
    [object[]]$Property = $(throw 'Supply properties'),
    [int]$Width = ($Host.UI.RawUI.BufferSize.Width/2),
    [char]$ForeChar = 9600,
    [char]$BackChar = 9617,
    [object[]]$InputObject
)

$set = $(if ($InputObject) {$InputObject} else {@($Input)}) |
Select-Object $Property

$max = ($set | Measure-Object ($Property[-1]) -Maximum).Maximum
if ($max -eq 0) {$max = 1}

$set | .{process{
    $_ | Add-Member -PassThru NoteProperty Chart (("$ForeChar"*(
    $_.$($Property[-1])/$max*$Width)).PadRight($Width, $BackChar))
}} |
Format-Table ($Property + 'Chart') -AutoSize


Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: