Skip to main content

Export Hash Tables to CSV - Powershell V2

I wrote about hash tables a while back in regards to counting OS types. The one thing I didn't lookinto back then was how to export the information to CSV. Easy, I hear you cry - just use export-csv!  Not quite as easy as you would imagine.

The main thing to tackle is the fact that Hash Tables are not objects as we would usually consider them.  If you run get-member on a hash table you get the following properties :
Count
IsFixedSize
IsReadOnly
IsSynchronized
Keys
SyncRoot
Values
What is required is to create an object based on the hash table prior to exporting. Lets create a similar hash table to the one found in my previous post :

$complist = get-ADcomputer -filter {enabled -eq $true} -properties operatingsystem
$OSlist = @{}
$complist | % {try {$OSlist.$($_.operatingsystem) ++}catch{$OSlist.None ++}}

Export this to CSV and you get :

PS U:\> $OSlist | Export-Csv "OScount1.csv" -NoTypeInformation
PS U:\> type .\OScount1.csv
"IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","21"
Not helpful really! Instead, convert the object like this :

new-object psobject -property $OSlist | Export-Csv "OScount1.csv" -NoTypeInformation
Better! At least we have data output now! But what I really wanted was 2 columns with lots of rows rather than 2 rows and lots of columns. This is how I worked around the issue :

$collection = @()
foreach ($key in $OSlist.Keys) {
   $store = "" | select "OS","count"
   $store.OS = "$Key"
   $store.count = $oslist.$Key
   $collection += $store
}
$collection | Export-Csv "OSCount2.csv" -NoTypeInformation

complete script :
$complist = get-ADcomputer -filter {enabled -eq $true} -properties operatingsystem
$OSlist = @{}
$complist | % {try {$OSlist.$($_.operatingsystem) ++}catch{$OSlist.None ++}}

$collection = @()
foreach ($key in $OSlist.Keys) {
   $store = "" | select "OS","count"
   $store.OS = "$Key"
   $store.count = $oslist.$Key
   $collection += $store
}
$collection | Export-Csv "OSCount2.csv" -NoTypeInformation
As a final thought, I looked at how to export multiple hash tables with multiple values out to a single CSV. This combine aspects from both the above solutions. I have emulated a 2 record foreach loop by writing the code twice for simplicity :
$collection = @()
$vars = @{}
$vars.Givenname = "Adam"
$vars.sn = "Stone"
$vars.address = "1 ABC Street"
$vars.City = "Some City"

$collection += new-object psobject -property $vars

$vars = @{}
$vars.Givenname = "John"
$vars.sn = "Citizen"
$vars.address = "2 ABC Street"
$vars.City = "Some other City"
$collection += new-object psobject -property $vars

$collection | Export-Csv "Vars2.csv" -NoTypeInformation
This Outputs :
PS U:\> type .\Vars2.csv
"City","sn","address","Givenname"
"Some City","Stone","1 ABC Street","Adam"
"Some other City","Citizen","2 ABC Street","John"

Cheers!

Comments