Create PowerShell Hashtable

PowerShell Hashtables are compact data structures, which are very popular and come handy in a lot of use cases. They are capable to store one or more Key-Value pairs and today we are going to looking into various approaches and methods to create a Hashtable in PowerShell:

  1. Using @{ }

    The simplest way to create a hashtable is using @ followed by curly-bracket pair, where brackets define the body of hashtable where key-value pairs can be defined separated by an EqualsTo sign = as demonstrated in the following example


  2. Using .Net class for Hashtable with New-Object cmdlet

    All the Hashtables you create using the above method would be created as an object .Net class System.Collections.Hashtable, that means you can use this class and instantiate it using the cmdlet New-Object to create a Hashtable.

  3. Using [Hashtable] PowerShell Type Accelerator

    We can also use the PowerShell Type accelerator of the .Net class with New() method to create an instance of the Hashtable object, which will save us from some extra typing and definitely looks neat, like in the following example:

    PowerShell Hashtable

  4. Using [Ordered] Dictionary

    Since PowerShell v3.0, you can now also use the [Ordered] attribute to create an Ordered Dictionary which makes sure keys appear in the order in which they are defined, under the hoods, this is forcing the hashtable to be created using the class [System.Collections.Specialized.OrderedDictionary] instead of [System.Collections.Hashtable].

    Now, we know that this class exists, it can be directly used to create Hashtables that will be ordered.

    PowerShell Hashtable

  5. Using [System.Collections.Generic.Dictionary[KeyType,ValueType]]

    To be honest, I’m cheating here a little, Dictionary and Hashtable are two different data structures, but they are very identical as both of them store a Key-Value pair, but dictionaries are Strongly typed unlike PowerShell [Hashtables], which means in a Dictionary if a key\value is defined to store an [int] value you can not add any other data type, otherwise you will run into errors.

    Dictionaries are usually faster because there is no boxing and unboxing happening like a Hashtable in PowerShell. In the following example, we are using the System.Collections.Generic.Dictionary[String,String] class with data type for key and the value to create a dictionary object.

    PowerShell Hashtable

  6. Using[Dictionary] PowerShell Type Accelerator

    You can use the PowerShell type accelerator of the System.Collections.Generic.Dictionary Class with New() method to create your dictionaries.

    PowerShell Hashtable

  7. From[PSObject] by iterating properties & adding Key-Value pairs

    We can access the properties of PowerShell Objects [PSObject], and iterate each property and its value and feed them in a Hashtable to initialize one, like in the following example.

    PowerShell Hashtable

  8. [String] to [Hashtable] using ConvertFrom-StringData cmdlet

    Hashtables are very popular in efficiently finding and retrieving data, and cmdlets like ConvertFrom-StringData make it further easy to extract data from a string directly into a hashtable as demonstrated in the following example. This approach will save you some extra typing and use of punctuations.

    PowerShell Hashtable

  9. From[String] using -Match operator and RegEx

    When dealing with strings data or information from a file, you can use the combination of a Regular Expressions and -Match operator to extract Key and Value pairs from data, which can be used to initialize and create Hashtables in PowerShell, this is one of my favorite ways to build a hashtable from a Log (*.log) or Settings (*.INI) file.

  10. Using Group-Object Cmdlet’s '-AsHashTable' switch parameter

    You can use the -AsHashTable switch parameter of Group-Object cmdlet to convert [Microsoft.PowerShell.Commands.GroupInfo] objects to [System.Collections.Hashtable]

  11. Using ConvertFrom-JSON Cmdlet’s '-AsHashTable' switch parameter

    Since PowerShell v6.2, ConvertFrom-JSON cmdlet also supports '-AsHashTable' switch parameter, which can be useful in a lot of scenarios. I was not aware of this, thanks to Kevin Marquette for this tip!PowerShell hashtable


Following are all the example we discussed in the above section which I could think of, let me know in case you know more in comments and I’ll add them into this article with credits. Thank you.

Examples: 


If you like this article read more similar articles under ‘N – Ways to’ category

 

Optical Character Recognition
~ Author of “PowerShell Guide to Python“,  and currently writing a Book on “Windows Subsystem for Linux (WSL)


All my books are available as a discounted bundle:

    1. PowerShell Guide to Python : This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer time.
    2. Windows Subsystem for Linux (WSL) Keywords, definitions, and problems WSL solve and how it works under the hoods. From download to setup to interoperability this book even covers details like the architecture of Windows subsystem for Linux and new features in WSL 2 with some wonderful use cases.

 


Subscribe to our mailing list

* indicates required