Friday Fun Convert Object to Hash Table

I’ve been working on a few PowerShell projects recently and one requirement I had was to turn an object into a hash table. I thought this was something that was already handled in PowerShell but I couldn’t find a cmdlet or an easy .NET technique. So I wrote my own function, ConvertTo-Hashtable.

The function is designed to accept pipelined input and turns each object into an associative array, i.e., a hash table, and write the hash table to the pipeline.

As an object is processed by the function it uses Get-Member to retrieve all of the object’s properties.

The function then walks the object and adds each property name and value to a hash table.

This hash table object, $hash, eventually is written to the pipeline. This process happens to each piped in object. However, I also added a function parameter, -NoEmpty, to filter out properties with no values. When specified, the function creates a second hash table and then checks each key in $hash. If the corresponding item has a value, the key and value are added to the secondary hash.

The secondary hash is then written to the pipeline. I had to take this approach because PowerShell doesn’t like it when you try to modify and enumerate a hash table at the same time. Here’s how you might use it:

Or to filter out the empty values:

Granted, your use case for something like this is probably pretty limited. But there are some good technique examples here on working with hash tables.

The one nagging problem, at least until PowerShell v3, is that it is very difficult to write a sorted hash table. I’d love to write the hash table to the pipeline with sorted keys, but that may have to wait.

In the meantime, feel free to download and experiment with ConvertTo-Hashtable.

3 thoughts on “Friday Fun Convert Object to Hash Table”

  1. “I’d love to write the hash table to the pipeline with sorted keys, but that may have to wait.”
    [cc lang="powershell"]
    Process {
    Write-Verbose “Converting an object of type $($_.GetType().Name)”
    #get propery names
    $names=$InputObject | get-member -MemberType properties | select-object -expandproperty name
    #define an empty hash table
    $hash= new-object system.collections.specialized.ordereddictionary
    #go through the list of names and add each property and value to the hash table
    $names | sort | foreach-object {
    Write-Verbose “Adding property $_”
    } #foreach
    if ($noEmpty) {
    Write-Verbose “Parsing out empty values”
    #define a new hash
    $defined=new-object system.collections.specialized.ordereddictionary
    #get items from $hash that have values and add to $defined
    $hash.keys | foreach-object {
    if ($hash.item($_)) {
    Write-Verbose “Writing the result to the pipeline”
    Write-Output $defined
    else {
    Write-Verbose “Writing the result to the pipeline”
    Write-Output $hash
    } #If $noempty
    }#close process
    }#end function

    1. I knew it could be done. Unfortunately it takes more knowledge than a typical IT Pro is likely to have. Thanks for your work.

Comments are closed.