Convert a Powershell hashtable to object
2015, May 12
I’ve struggled on a few occasions of being lumbered with a hashtable that i’d created for whatever reason (usually because it’s so damn quick and easy).
Having my roots in OO development, I inevitably want to start dealing with objects at some point.
Long story short, here you go 🙂
function ConvertHashtableTo-Object {
[CmdletBinding()]
Param([Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
[hashtable]$ht
)
PROCESS {
$results = @()
$ht | %{
$result = New-Object psobject;
foreach ($key in $_.keys) {
$result | Add-Member -MemberType NoteProperty -Name $key -Value $_[$key]
}
$results += $result;
}
return $results
}
}