Table of Contents
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.
1 2 3 4 5 6 7 8 9 |
$hashTable = @{ 'Name' = 'PowerShell' 'Version' = 7.3 } $value = $hashTable['Name'] Write-Output $value |
1 2 3 |
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.
1 2 3 4 5 6 7 8 9 |
$hashTable = @{ 'Name' = 'PowerShell' 'Version' = 7.3 } $value = $hashTable.Item('Name') Write-Output $value |
1 2 3 |
PowerShell |
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.
1 2 3 4 5 6 7 8 9 |
$hashTable = @{ 'Name' = 'PowerShell' 'Version' = 7.3 } $value = $hashTable.Name Write-Output $value |
1 2 3 |
PowerShell |
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.
1 2 3 4 5 6 7 8 |
$hashTable = @{ 'Name' = 'PowerShell' 'Version' = 7.3 } $hashTable.GetEnumerator() | Where-Object {$_.Key -eq 'Name'} |
1 2 3 4 5 |
Name Value ---- ----- Name PowerShell |
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.
1 2 3 4 5 6 7 8 |
$hashTable = @{ 'Name' = 'PowerShell' 'Version' = 7.3 } ($hashTable.GetEnumerator() | Where-Object {$_.Key -eq "Name"}).Value |
1 2 3 |
PowerShell |
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
OperatorForEach-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.
1 2 3 4 5 6 7 8 9 |
$hashTable = @{ 'FirstName' = 'John' 'LastName' = 'Williamson' 'Course' = 'PowerShell' } $hashTable.GetEnumerator() | Where-Object {$_.Key -match 'Name'} |
1 2 3 4 5 6 |
Name Value ---- ----- FirstName John LastName Williamson |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$hashTable = @{ 'Name' = 'PowerShell' 'Version' = 7.3 'OS' = 'Windows' } $hashTable.GetEnumerator() | ForEach-Object { if ($_.Key -eq 'Version') { Write-Output $_.Value } if ($_.Key -eq 'OS') { Write-Output $_.Value } } |
1 2 3 4 |
7.3 Windows |
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.
1 2 3 4 5 6 7 8 9 10 |
$hashTable = @{ 'Name' = 'PowerShell' 'Version' = 7.3 } $keys = @('Name','Version') $values = $hashTable[$keys] Write-Output $values |
1 2 3 4 |
PowerShell 7.3 |
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
LoopWhere-Object
Cmdlet
Use ForEach
Loop
Use the ForEach
Loop to get all values with respective keys of a hash table in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 |
$hashTable = @{ 'Name' = 'PowerShell' 'Version' = 7.3 'OS' = 'Windows' } ForEach($key in $hashTable.Keys){ $message = '{0} = {1}' -f $key, $hashTable[$key] Write-Output $message } |
1 2 3 4 5 |
Name = PowerShell Version = 7.3 OS = Windows |
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:
1 2 3 |
$hashTable |
1 2 3 4 5 6 7 |
Name Value ---- ----- Name PowerShell Version 7.3 OS Windows |
Or
1 2 3 |
$hashTable.GetEnumerator() |
1 2 3 4 5 6 7 |
Name Value ---- ----- Name PowerShell Version 7.3 OS Windows |
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.
1 2 3 4 5 6 7 8 9 |
$hashTable = @{ 'Name' = 'PowerShell' 'Version' = 7.3 'OS' = 'Windows' } $hashTable.PSObject.Properties | Where-Object {$_.Name -eq "Values"} | Select Value |
1 2 3 4 5 |
Value ----- {PowerShell, 7.3, Windows} |
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
$hashtable = @{ 'StudentID' = 11239 'Name' = @{ 'FirstName' = 'John' 'LastName' = 'Williamson' } } $value = $hashtable['Name']['FirstName'] Write-Output $value |
1 2 3 |
John |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$hashtable = @{ 'StudentID' = 11239 'Addresses' = @{ 'permanentAddress' = @{ 'City' = 'Lahore' 'Country' = 'Pakistan' } 'temporaryAddress' = @{ 'City' = 'Multan' 'Country' = 'Pakistan' } } } $hashtable['Addresses']['permanentAddress']['City'] $hashtable['Addresses']['temporaryAddress']['City'] |
1 2 3 4 |
Lahore Multan |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function GetValueByKey { param ( [hashTable] $hashTable, [string] $key ) $hashTable.GetEnumerator() | ForEach-Object { if ($_.Key -eq $key) { Write-Output $_.Value } elseif ($_.Value -is [hashTable]) { Get-ValueByKey $_.Value $key } } } $hashTable = @{'Name' = 'PowerShell'; 'Version' = 7.3; 'OS' = 'Windows'} GetValueByKey $hashTable 'Name' |
1 2 3 |
PowerShell |
For this approach, we created a function called GetValueByKey
that took in two parameters:
$hashTable
– a hash table object$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.