PowerShell Out-ExcelPivotTable – Quickly Create Pivot Tables in Microsoft Excel

June 24, 2009

in Excel,Pivot Tables,PowerShell

** Updated Code 06/03/2010 **

Bradley requested two features. How to save the pivot table to a file and close excel.

Download the new scripts below and here is how to use it.

$data | .\Out-ExcelPivotTable -saveAs "$pwd\data.xls" -autoCloseXl

Thanks for the feature request Bradley.

The Data – People.csv

image

Use Import-Csv and type the data with Select

image 

Create the Excel Pivot Table

$people | .\Out-ExcelPivotTable

 

image

The easiest way to get a Pivot Table.

Out-ExcelPivotTable inspects the piped data, sets up the numeric properties as data fields and string properties as row fields (nested).

 

 

$people | .\Out-ExcelPivotTable name dept salary

 

image

Same data.

You control what goes in the different pivot fields. Row (name), Column (dept) and Data (salary).

 

 

 

$people | .\Out-ExcelPivotTable -values YearsEmployeed

 

image

You control the pivot data field with the –values parameter letting  Out-ExcelPivotTable do the rest.

Name is nested in Department.

 

 

 

 

ToDo

  • Set up Out-ExcelPivotTable parameters to take arrays
  • Formatting of value fields
  • Naming of Row and Column Labels
  • Enable Types of aggregates for Value field
  • Improve data layout performance

I have only tried this on the latest version of Excel.

Download It

{ 1 trackback }

Tweets that mention PowerShell Out-ExcelPivotTable – Quickly Create Pivot Tables in Microsoft Excel -- Topsy.com
01.03.11 at 6:40 pm

{ 6 comments… read them below or add one }

Grant 06.28.09 at 8:19 am

Pretty Schweet !!!

Andy 07.23.09 at 10:13 pm

Another gem !

Yong 05.14.10 at 6:53 pm

Wonderful! But make sure you’re on Powershell 2.0 everyone.

Bradley 06.03.10 at 12:57 pm

I hope someone sees this… how can i make it automatically save the pivot table to a file and close excel.

David 05.24.11 at 10:26 pm

Excellent Tutorial. Hats off to you..

compulsive scripter 10.14.11 at 3:17 am

Great Script! Another useful tip is formatting the data type-ing it:

$events = Import-Csv .\events.csv |
select @{
n=”Date”
e={“{0:dd MMM}” -f [DateTime]$_.Date}
}, @{
n=”Start”
e={“{0:HH:mm}” -f [DateTime]$_.Start}
}, @{
n=”Minutes”
e={“{0:N0}” -f [Double]$_.Minutes}
}

Question – is there a similar COM call to make a pivot chart from this? Couldn’t find anything obvious (was hoping for CreatePivotChart()… :)

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Contrat Creative Commons

© 2007-2012, Doug Finke