Hashtables are something you use regularly, but there is a Module to help with certain operations, such as merging and converting to PSCustomObject. In this small blog post, I will show you how it works.
What are Hashtables?
“A hashtable, also known as a dictionary or associative array, is a compact data structure that stores one or more key-value pairs. For example, a hashtable might contain a series of IP addresses and computer names, where the IP addresses are the keys and the computer names are the values, or vice versa.
In PowerShell, each hashtable is a [System.Collections.Hashtable] object. You can use the properties and methods of Hashtable objects in PowerShell.
Beginning in PowerShell 3.0, you can use the [ordered] type accelerator to create an [System.Collections.Specialized.OrderedDictionary] object in PowerShell.
Ordered dictionaries differ from hashtables in that the keys always appear in the order in which you list them. The order of keys in a hashtable isn’t deterministic.
The keys and value in hashtables are also .NET objects. They’re most often strings or integers, but they can have any object type. You can also create nested hashtables, in which the value of a key is another hashtable.
Hashtables are frequently used because they’re efficient for finding and retrieving data. You can use hashtables to store lists and to create calculated properties in PowerShell. And, the ConvertFrom-StringData cmdlet converts structured string data to a hashtable.”
The Hashtable module
Marius Storhaug created the Hashtable module. It’s a comprehensive module that provides functions for working with PowerShell hashtables. It enables you to convert hashtables to PSCustomObjects and vice versa, format hashtables into readable PowerShell code, merge hashtables with override support, and remove entries based on specific criteria. This collection of utilities is designed to simplify complex hashtable manipulations and help automate your PowerShell workflows.
Installation
You can install it from the PowerShell Gallery using:
Install-PSResource -Name Hashtable
Or, if you don’t have the PSResourceGet module installed, you can install it using:
Install-Module -Name Hashtable
After installation, these Cmdlets are available: (Join-Hashtable is an alias of Merge-Hashtable)

Using the Module
ConvertFrom-Hashtable
This allows you to convert from a Hashtable to a PSCustomObject. For example:

You can see that the Hashtable (And yes, I added Weather with the hot value because it’s like 30 Celsius inside now in the Netherlands! 😀 ) is converted to a PSCustomObject.
ConvertTo-Hashtable
When you use ConvertTo-Hashtable, it will convert an Object to a Hashtable. For example:

I created a PSCustomObject with the same values from scratch and converted that to a Hashtable using ConvertTo-Hashtable.
Export-Hashtable
This exports a Hashtable to PSD1, PS1, or JSON format, but not to any other formats. For example:

I exported it to different formats, choosing another extension, like .txt, will throw an error:

Format-Hashtable
This will make exported Hashtables easier to read. For example:

It’s readable as a Hashtable, but using Format-Hashtable makes it more readable 🙂
Import-Hashtable
This allows you to import a PSD1, PS1, or JSON file as an (Ordered) Hashtable. For example:

I imported the files from two chapters ago and stored the last one (hashtable_output.json) in a Variable $hashtable, which is an OrderedHashtable.
Merge-Hashtable
This will merge the two separate Hashtables into one. It can do that with and without overwriting duplicate keys. For example:

The second Hashtable, $Hashtable2, was merged into one and didn’t overwrite any values, but it added the unique Extra key with the value Value. In the example below, I specified the main Hashtable to be $Hashtable1 and let $Hashtable2 override any unique keys with the ones from that Hashtable using the -Overrides Parameter:

Remove-HashtableEntry
This Cmdlet will let you remove Null or Empty Values, or keep them if you want, remove certain types, keys, and all keys and values 🙂 For example:

In the example above, I created a HashTable $hashtable and removed all Booleans from it. Then I removed all empty-valued keys, then removed the Key Temperatures, and finally removed everything except the PowerShell and Learning keys. Very powerful!
More information
For more information about using the Hashtable module, please visit https://psmodule.io/Hashtable/.
Wrapping up
And that’s how you use the Hashtable Module and its Cmdlets, making life a lot easier working with Hashtables and converting them back and forth, etc. Have a lovely weekend!