Export Dynamic Data to a CSV using PowerShell

This post explains how to export dynamic data to a CSV using PowerShell.  I find that hashtables are quite useful for various scenarios when scripting in PowerShell.  We can also utilise them when exporting data using Export-CSV.

I provide two approaches to this.  Both approaches work by adding hashtables to an array.  By default if you export an array of hashtables to an array you will lose the column ordering.  With PowerShell 2 we can circumvent this issue by using ‘Select’ and specifying the order of the columns we would like to select:

With PowerShell 3 we can simplify this, by specifying the [ordered] type for our hashtable:

 

Leave a Reply

Your email address will not be published. Required fields are marked *