Visualize your PowerShell reports with PowerShell charts

I am so excited to share with you PowerShell charts. It took me a lot of time and effort to generate charts as a result of running PowerShell script.

I think one of the most interesting things when writing PowerShell scripts, is to make the results shine by including visuals and charts. If you are going to show disk space info, nothing more than a nice chart, will worth looking at. If you are sending a report to your management, nothing will take their attention more than charts. I love getting charts as a high level output, and perhaps put some tables or attach a csv file for drilling down.

Why this is different than others?

Because this script is a wrapper around the dirty stuff that you do not want to worry about, or may be you are new to PowerShell and  just what the chart to get generated. This script is so powerful that you can give it some data, choose the type of chart you want to generate, and guess what, you are done!

To make this happen, the machine in which you running your PowerShell scripts, should have two components:

Microsoft chart controls is the components that get invoked by this wrapper function, to generate the outcome chart. It is a light component, and there is no harm installing it.

Script details

This wrapper function wants some information from you. It needs an X-axis and Y-axis. I thought a lot of how I can feed the script those coordinates, and I realized it is best to construct the input outside the wrapper.

To do that, the wrapper will expect a parameter called DATA. This parameter expects an array of objects. Each object should have at least two properties that can represent an X-axis and Y-axis.

The wrapper function then expect a parameter called Obj_Key which is the name of the object property that should be mapped to the X-axis and Obj_Value to learn which object property to be considered Y-axis.

It might seem complex but believe me it is so easy. I will give you a simple example:

Suppose that you want to have a chart that represents 3 cities and their population. So you will have an excel sheet with two columns City_Name and City_Population

PowerShell charts 1

To create a chart for this type of input data, we need to create a PowerShell object per city, that has two properties:

  • City_Name
  • City_Population
#creating array that holds all objects
$array_of_city_objects = @()

#object properties or the X-axis and Y-axis in the chart
$properties = @{ City_Name = ""
                 City_Population = 0}

#Creating London City object
$city1 = New-Object -TypeName psobject -Property $properties
$city1.City_Name = "London"
$city1.City_Population = 15000000

#Creating London City object
$city1 = New-Object -TypeName psobject -Property $properties
$city1.City_Name = "London"
$city1.City_Population = 15000000

#Creating Madrid City object
$city1 = New-Object -TypeName psobject -Property $properties
$city1.City_Name = "Madrid"
$city1.City_Population = 600000
$array_of_city_objects+= $city1

#Creating Berlin City object
$city1 = New-Object -TypeName psobject -Property $properties
$city1.City_Name = "Berlin"
$city1.City_Population = 5420000
$array_of_city_objects+= $city1

#Creating London City object
$city1 = New-Object -TypeName psobject -Property $properties
$city1.City_Name = "London"
$city1.City_Population = 15000000
$array_of_city_objects+= $city1

#$array_of_city_objects now holds three objects, each object has two properties $city_name and $city_population

Get-Corpchart-LightEdition -data $array_of_city_objects  -obj_key "City_Name" -obj_value "City_Population" -FilePath C:\graph.png

PowerShell charts 2

Script features

There are new features that are added to the wrapper function script:

  • Sortable data option via -sort parameter.
  • Fix label alignment via -fix_label_alignment parameter.
  • Exposing X and Y axis intervals via parameters.
  • Show data as percentage via -show_percentage_pie parameter.
  • Collected threshold to group data items below the threshold into one item called ‘Others‘.
  • Customizing chart data column colors via -chart_color parameter.
  • Dynamic dimension depending on the number of items in the input data.

You can download the script from Microsoft TechNet Gallery.

PowerShell charts 3


PowerShell charts 4


PowerShell charts 5


PowerShell charts 6


PowerShell charts 7


PowerShell charts 8


You can decide which PowerShell charts type to produce. There are alot of chart types available as part of the Microsoft Chart Controls for Microsoft .NET Framework 3.5. Specifying chart type as pie chart type can be accomplished by running this command:

PS C:\> Get-Corpchart-LightEdition -data  $cities  -obj_key "Name" -obj_value "Population" -filepath "c:\chart.png" -type Pie

You can also decide if you want to show the legend by using the -showlegend parameter.

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key "Name" -obj_value "Population" -filepath "c:\chart.png" -type Pie -showlegend

You can decide which PowerShell charts type to produce . For example, to use the SplineArea chart type instead of the pie chart:

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key "Name" -obj_value "Population" -filepath "c:\chart.png" -type SplineArea

You can decide which PowerShell charts type to produce. For example, here we will be specifying chart type as Bar chart type, and specifying the title for the chart using the -title parameter, and specifying the name of each axis on the chart by using the -chartarea_Xtitle  and  –chartarea_Ytitle parameters:

PS C:\> Get-Corpchart-LightEdition -data $cities  -obj_key  "Name" -obj_value  "Population" -filepath "c:\chart.png" -type Bar -title_text  "people per country" -chartarea_Xtitle  "cities" -chartarea_Ytitle  "population"

You can decide which PowerShell charts type to produce. For example, here we will be specifying chart type as Column chart type. Applying the -showHighLow switch to highlight the max and min values with different colors. This means that if you have like 12 cities with their populations, the lowest and highest cities will be highlighted.

PS C:\> Get-Corpchart-LightEdition -data  $cities  -obj_key  "Name"  -obj_value  "Population" -filepath  "c:\chart.png"  -type  Column  -showHighLow

You can also include the percentage of the values, instead of the values themselves if you are working on the Pie or Doughnut chart types:

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key "Name" -obj_value "Population" -filepath "c:\chart.png" -type Doughnut -Show_percentage_pie

If the chart type is Pie or Doughnut, you can specify a threshold (percentage) that all data values below it, will be shown as one data item called (Others). Say for example you have like 100 cities with their population. There are almost 85 cities with population under 1 million. You want all those 85 cities to be represented as one city called Others, by setting 1 million as a threshold. The chart then will show 16 cities only instead of 100 cities, focusing thus on those with over 1 million population.

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key "Name" -obj_value "Population"  -filepath "c:\chart.png" -type Doughnut  -CollectedThreshold 1000000

You can color your chart columns if you are using the Column chart type. To color columns with green:

PS C:\> Get-Corpchart-LightEdition -data $cities -obj_key "Name" -obj_value "Population" -filepath "c:\chart.png" -chart_color Green