Converting Hash Tables to Objects

by Nov 14, 2008

Hash Tables are convenient but are not true objects. This is bad because you are unable to output the hash content to formatting cmdlets or export cmdlets. With a short function, you can easily convert a Hash Table to an object, providing all the flexibility you need to forward the object to other cmdlets like Format-Table or Export-CSV:

function ConvertTo-Object($hashtable) 
$object = New-Object PSObject
$hashtable.GetEnumerator() |
ForEach-Object { Add-Member -inputObject $object `
-memberType NoteProperty -name $_.Name -value $_.Value }

$hash = @{Name='Tobias'=='Online'}
ConvertTo-Object $hash