PowerShell – Array of HashTables

Array of HashTables in PowerShell

Creating an Array of HashTables

To create an array of hashtables in PowerShell:

  • Create an array.
  • Create two or more hash tables.
  • Use += operator to add the hash tables (created in the second step) to an array (created in the first step).

First, we used an array operator represented with @() to create an array and saved it in the $arrayOfHashTables variable. Next, we created two hash tables stored in $hashTable1 and $hashTable2; each contained key-value pairs wrapped around @{}. Optionally, we can use [ordered] to have key-value pairs in the same order in which they are inserted while creating the hash tables. The array of hash tables will look as follows if we will not use [ordered].

Once we are done with creating hash tables, we use the += operator to add $hashTable1 and $hashTable2 in the $arrayOfHashTables. Alternatively, you can also use the + operator to add hash tables to the array of hash tables as follows:

We created two hash tables ($hashTable1 and $hashTable2); now, what if we have to add 10 or 15 more? What will happen now? It will result in code redundancy. To avoid that, we will write this in a script named Test.ps1 and pass one or multiple hash tables as parameters. How to do it? Let’s learn that in the following section.

Passing Data from a Parameter to an Array of HashTable

To pass data from a parameter to an array of hash tables:

  • Create a .ps1 file, let’s say Test.ps1.
  • Use the param block to accept an array of hash tables as a parameter.
  • Use the Write-Host cmdlet to display customized messages.
  • Use the foreach loop to iterate over an array of hash tables.
  • Use nested foreach with the GetEnumerator() method to iterate over each hash table entry.
  • For each nested iteration of the above loop, use the Write-Host cmdlet to write the key and its value in key=value format.

First, we used the param block with the param keyword followed by opening, and closing brackets (()); this block accepts one or more parameters. We used this to take an array of hash tables as a parameter.

The point is how we can ensure that this script only takes an array of hash tables as a parameter. We can assign a type to the parameter and include a [Parameter()] block. In the following example, we can see a parameter that only accepts an array of hash tables of type hashtable[].

We used Write-Host cmdlet on different places in the above-provided script (Test.ps1) to write customized output/message to the host; here host is our PowerShell console or terminal.

Next, a foreach loop was used to iterate over an array of hash tables ($Hashtables). In each iteration, we used a nested foreach with the GetEnumerator() method to loop over every entry of the hash table and write key=value using Write-Host cmdlet.

What is the role of the GetEnumartor() method? This method was used to get an enumerator (a IEnumerator object) that we used to iterate through the specified collection, which is a hash table in our case.

That’s all about array of HashTables in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *