PowerShell – Get Value by Key in HashTable

Get Value by Key in PowerShell

Getting Single Value by Single Key

We can get a single value by a single key in a hash table using the following ways in PowerShell:

  • Indexer Notation
  • .Item() Method
  • Dot Notation
  • Where-Object Cmdlet

Use Indexer Notation

Use indexer notation represented with square brackets ([]) to get a value by key in HashTable in PowerShell.

We created a hash table with two key-value pairs, Name with PowerShell & Version with 7.3, and saved it in $hashTable. Then, we used indexer notation ([]) by specifying the key to get its value, which we saved in the $value variable. Finally, we used Write-Output cmdlet to display the value of the $value variable on the console.

We can also use the Write-Output cmdlet to write the given objects to the pipeline. If this cmdlet is the only command or the last command in the pipeline, the objects will be displayed on the console. It sends objects to the primary pipeline, the output stream, also known as the success pipeline; we typically use this cmdlet to display objects and strings on the console.

Use .Item() Method

Use the .Item() method to get a value by key in the hash table.

This code example uses the same hash table as the previous code. Here, we used the .Item() method, which took a key as an argument and returned the respective value that we stored in the $value variable.

This $value was later used to print its value on the console using the Write-Output cmdlet. Using .Item() is not a different approach but a convenient alternative to indexer notation.

Use Dot Notation

Use dot notation represented with . to get a value by key in the hash table.

The above example is also similar to the last two code snippets, using the same hash table and writing retrieved value using the Write-Output cmdlet. The only difference is that we used dot notation to access the value of the specified key.

Use Where-Object Cmdlet

Use the Where-Object cmdlet to get a value by key in the hash table.

For this code, we used the GetEnumerator() method to get an enumerator for the $hashTable, which was used to iterate over the key-value pairs of the hash table. Then, the enumerator was piped into the Where-Object cmdlet, which filtered the hash table based on the specified condition $_.Key -eq 'Name' to return only the key-value pairs where the key is equal to 'Name'. Remember, -eq is the case-insensitive operator.

It returned an object containing the key-value pair for key 'Name' with the value 'PowerShell' (see the above output). We can use the above solution when we want to filter the key-value pairs of a hash table based on a specific key. If you are interested in the value only, then you can use the .Value property as given below.

Getting Multiple Values by Multiple Keys

We can use the following different approaches in PowerShell to get multiple values by multiple keys in the hash table:

  • -match Operator
  • ForEach-Object Cmdlet
  • Array of Keys

Use -match Operator

Use the -match operator to get multiple values by multiple keys of a hash table in PowerShell.

This code example is the same as the one where we used the -eq operator to get a value by a key, but we replaced the -eq with the -match operator to filter multiple keys matching the pattern Name. Note that the -match operator is case-insensitive, meaning Name, name, and NAmE are all the same.

Use ForEach-Object Cmdlet

Use ForEach-Object Cmdlet to get multiple values by multiple keys in the hash table using PowerShell.

Again, we created a hash table having three key-value pairs. Next, we used the GetEnumerator() method to get an enumerator object to iterate over each key-value pair of the $hashTable.

This enumerator was then piped with the ForEach-Object cmdlet to loop over the key-value pairs, and inside the script block (represented with {}), it checked if the key is equal to 'Version' or 'OS' using -eq operator.

If it is True, it writes the value of the specified key using the Write-Output cmdlet. This solution can be helpful when you want to filter specific key-value pairs from a hash table and output their values. However, keep in mind that ForEach-Object will iterate through all key-value pairs; if you have a large hash table, this can be an expensive operation in terms of performance.

Use Array of Keys

Use an array of keys to get multiple values by multiple keys in the hash table.

Here, we used an array operator represented with @() to create an array containing keys ('Name' and 'Version') as array elements. Then, we saved this array in the $keys variable, passed in indexer notation ([]) to get an array of values, which we saved in the $values array for later use.

NOTE: It is not mandatory to use the array operator to create an array, but you can do it using $keys = ('Name', 'Version') or $keys = 'Name', 'Version' as well.

Getting All Values with/without Keys

We can use the following approaches in PowerShell to get all values with/without keys in the hash table:

  • ForEach Loop
  • Where-Object Cmdlet

Use ForEach Loop

Use the ForEach Loop to get all values with respective keys of a hash table in PowerShell.

Here, we used ForEach statement, also referred to as ForEach loop to traverse over a collection of all keys returned by $hashTable.Keys.

For each key, we created a string in the format "key = value" using the string formatting operator (-f), where "key" was the current key in the loop and "value" was the value associated with that key in the hash table. We then used the Write-Output cmdlet to output the message to the console.

Note that there are more compact forms giving us the same results with a header for a clear understanding. For that, we can use the following:

Or

Use Where-Object Cmdlet

Use the Where-Object cmdlet to get all values without keys of a hash table in PowerShell. This approach is useful when we want to get all values in the form of an object.

After creating a hash table with three key-value pairs, it accessed the PSObject property of the $hashTable object and its Properties collection. Then, it is piped to Where-Object to filter the properties collection by the property name 'Values' and then selects the Value property of the filtered object. To understand the above command, look at the following screenshot.

PowerShell HashTable Get Value by Key - HashTable PSObject Properties

Alternatively, we can use $hashTable.Values to get values only; this command is more straightforward.

Until this point, we have learned how to get a value by a key, multiple keys by multiple values, and all values with and without keys of a hash table; what if we are required to get value(s) from a hash table containing nested keys? Let’s see how we can tackle it.

Getting Nested Value of Nested Key

We can use the following ways in PowerShell to get the nested value of nested keys in the hash table:

  • Indexer Notation
  • User-defined Function

Use Indexer Notation

Use indexer notation to get a value of a nested key in the hash table using PowerShell.

We have already learned about indexer notation in the very first method of this article. For the above code, we used it with nested keys, which means it first located the 'Name' key, then in the 'Name' key, it located the 'FirstName' to retrieve the value. We can add more indexer notations to dive deeper. For example:

Use User-defined Function

To get a value by a key in the hash table using PowerShell:

  • Create a function named GetValueByKey to search the value of the key even if the value is of nested hash table
  • Create a hash table.
  • Call the function created in the first step by passing two arguments; the first is the hash table object, and the second is the key for which you want to retrieve a key.

For this approach, we created a function called GetValueByKey that took in two parameters:

  1. $hashTable – a hash table object
  2. $key – a string value representing the key to search for in the hash table

The function then used the GetEnumerator() method of the hash table object to iterate through each key-value pair in the hash table. For each pair, it checked if the key was equal to the searched key, and if so, it wrote the corresponding value to the output using the Write-Output cmdlet.

If the value for the key is a hash table, the function will recursively call itself, passing the nested hash table and the search key as arguments. This also allowed the function to search for the key in nested hash tables. This function will search the key’s value even if the value is of a nested hash table.

It is worth noting that the function will output all the matching key values it finds from the input hash table and its nested hash table. If you want only the first matching key value, you can replace the Write-Output cmdlet with a return statement.

That’s all about how to get value by key in PowerShell.

Was this post helpful?

Leave a Reply

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