PowerShell Graphing with Out-Gridview

I’ve received a lot of interest for my last few posts on graphing with the PowerShell console. But I decided I could add one more feature. Technically it might have made more sense to turn this into a separate function, but I decided to simply modify the last version of Out-ConsoleGraph. The new version adds a switch parameter called -Gridview. When used, the graphing information is sent to the Out-Gridview cmdlet. This version will require PowerShell 3.0 as I’ll explain.

I realized I could just as easily send the values for the object name, property and graphing figure to Out-Gridview. In the ForEach construct where each object is processed, if the -GridView parameter is specified, I’ll create an ordered hash table and add it to an array.


...
if ($GridView) {
#add each object to the gridview data array
$gvHash = [ordered]@{
$CaptionProperty = $caption
$Property = ($g*$graph)
Value = $obj.$Property
}
$gvData += New-Object -TypeName PSObject -Property $gvHash
}
...

I need an ordered hash table so that the property values remain in the same order. I could have piped the new object to Select-Object and specified the order, but hopefully many of you are moving to v3 anyway. There’s no need for me to pad the caption since the grid view lines things up in columns automatically. It is also easier to add a property with the actual property value. I wanted to incorporate that value into the console version, but it proved to be more complicated than expected. Using Out-Gridview is a nice alternative.

After all of the objects have been processed, if there is anything in $gvData, I send it Out-Gridview.


...
if ($gvData) {
Write-Verbose "Sending data to Out-Gridview"
$gvData | Out-GridView -Title $Title
}
...

When using the grid, there is no way to specify color. But on the other hand, it is much easier to click on a column heading and re-sort. Here’s a command I ran with the new function.


Get-ChildItem C:\Scripts -Directory | foreach {
$data = Get-ChildItem $_.FullName -Recurse -File | Measure-Object -Property Length -sum
$_ | Select Name,@{Name="Size";Expression={$data.sum}}
} | Out-ConsoleGraph -Property Size -Title "Scripts Folder Report" -grid

out-consolegraph-gv

Now I have a tool with some flexibility. Although you might also be interested in this article from last November on building a drive report graph also using Out-Gridview.

I bumped the latest version of Out-Consolegraph to 3.0 to align it with the file name. Download Out-ConsoleGraph-v3

PowerShell Console Graphing Revised

Many of you have been having fun with my PowerShell Console Graphing tool I posted the other day. But I felt the need to make one more major tweak. I wanted to have the option for conditional formatting. That is, display graphed entries with high values in one color, medium in another and low in yet another.

The default behavior is still to use a single color. But using ParameterSets I added some new parameters, -HighColor, -MediumColor and -LowColor. They are all mandatory so if you use one parameter you have to define them all.

I also moved the Property parameter and made it positional which should make it easier to use. The conditional coloring works basically by taking the largest possible graph value, which is based on available screen width and dividing it into thirds. The top third is considered high, second third is medium and last third is low.

When it comes time to graph, I check which parameter set we’re using and set the graph color accordingly.

But now I can run a command like this:

out-consolegraph-3

What do you think? Download Out-ConsoleGraph-v2.

UPDATE: A newer version is available at http://jdhitsolutions.com/blog/2013/12/updated-console-graphing-in-powershell/

Graphing with the PowerShell Console

I’ve written before about using the PowerShell console as a graphing tool, primarily using Write-Host. Most of what I’ve published before were really proof of concept. I decided to try and come up with a more formal and re-usable tool that could create a horizontal bar graph based on a numeric property from piped objects. For example, I wanted to get all processes and display a graph of the WorkingSet for each object. My result is an advanced function that should work in v2 or v3 called Out-ConsoleGraph.

Here’s the code, minus, the comment-based help. I’ve commented the quote quite a bit so I won’t spend a lot of time explaining it in detail.

The function requires that PowerShell be running in STA mode, which shouldn’t really be an issue. The intent is that you will be piping objects to the function. You need to specify a property that you want to graph and an object property to use as the label or caption for each object. The default caption is the Name property which seems pretty common. The property you are graphing must have a numeric value. The function’s premise is to get the window width, then write the caption and a graph figure using the remaining available width. The function has a bit of code to calculate the longest caption value so that everything lines up and then determines how much space remains for graphing.

The graph is really more of a proportional representation as opposed to actual value. In short, I find the largest property value which essentially becomes the 100% mark. All other values are calculated as percentages and graphed accordingly. This might be easier to understand if you see it in action.

This is getting all non-Microsoft processes and creating a graph of the WorkingSet property.

out-consolegraph-1

The graph title and color are customizable via parameters. This should work for any type of object as long as you can have a numeric property.

out-consolegraph-2

This command will work in the PowerShell ISE but I think it works better in the PowerShell console. Remember, this command is NOT writing to the pipeline so all you can do is view the output.

Download Out-ConsoleGraph and let me know what you think.