Table of Contents
Using ContainsKey() Method
To check if HashTable contains a key in PowerShell,Use the if statement with the .ContainsKey() method.
|
1 2 3 4 5 6 7 8 9 |
$hashTable = @{'Name'='PowerShell'; 'Version'=7.0} $key = 'Name' if($hashTable.ContainsKey($key)){ Write-Host "Key '$key' exists in the hash table." }else{ Write-Host "Key '$key' does not exist in the hash table." } |
|
1 2 3 |
Key 'Name' exists in the hash table. |
We assume you have enough knowledge about creating hash tables in PowerShell but let us briefly explain to make everyone comfortable with this article. The easiest method to create a hash table in PowerShell is to use @{} syntax as follows:
|
1 2 3 4 5 6 7 8 |
$YourHashTableName = @{ 'key1' = 'Value' 'key2' = 'Value' 'key3' = 'Value' 'key4' = 'Value' } |
We can use a line break or a semi-colon (;) to separate each key-value pair from the other. You can find more details about hash tables here. In the above code, we create a hash table named $hashTable with two key-value pairs: 'Name' = 'PowerShell' and 'Version' = 7.0.
Then, we created and initialized the $key variable containing a key (which is 'Name' in this case) and used the .ContainsKey() method to assess if the key stored in the $key variable exists in the $hashTable. The .ContainsKey() method returns True if the key is found in the hash table; otherwise, False.
We enclosed the $hashTable.ContainsKey($key) within the if statement to display a custom message using the Write-Host cmdlet. If the if statement is True/False, we inform the user that your given key exists/doesn’t exist in the hash table respectively by displaying a message on the console.
This approach allows us to quickly check for the existence of different keys in a hash table by simply changing the value of the $key variable, which makes it more reusable. However, it is essential to note that the .ContainsKey() method is case-insensitive by default.
|
1 2 3 4 5 6 7 8 9 10 11 |
$hashTable = @{'Name'='PowerShell'; 'Version'=7.0} $keys = ('name', 'Name', 'Version', 'Names') for ($i = 0; $i -lt $keys.Length; $i++){ if($hashTable.ContainsKey($keys[$i])){ Write-Host "Key '$($keys[$i])' exists in the hash table." }else{ Write-Host "Key '$($keys[$i])' does not exist in the hash table." } } |
|
1 2 3 4 5 6 |
Key 'name' exists in the hash table. Key 'Name' exists in the hash table. Key 'Version' exists in the hash table. Key 'Names' does not exist in the hash table. |
We had an $keys array containing four keys for the above code. We used a for loop to iterate over this array; in each iteration, we used the .ContainsKey() method to check every key. Finally, the if-else was used to display different messages based on if the specified key is/isn’t found in the hash table.
We know how the $hashTable is created and why we use the if-else block. From now on, we will not go into the details by explaining everything but will focus on the method/operator used in that approach.
Further reading:
Using the -contains Operator
To check if HashTable contains a key in PowerShell, use the if statement with the -contains operator.
|
1 2 3 4 5 6 7 8 9 |
$hashTable = @{'Name'='PowerShell'; 'Version'=7.0} $key = 'Name' if($hashTable.Keys -contains $key){ Write-Host "Key '$key' exists in the hash table." }else{ Write-Host "Key '$key' does not exist in the hash table." } |
|
1 2 3 |
Key 'Name' exists in the hash table. |
Alternatively, we can use the .Contains() method to do the same.
|
1 2 3 4 5 6 7 8 9 |
$hashTable = @{'Name'='PowerShell'; 'Version'=7.0} $key = 'Name' if($hashTable.Keys.Contains($key)){ Write-Host "Key '$key' exists in the hash table." }else{ Write-Host "Key '$key' does not exist in the hash table." } |
|
1 2 3 |
Key 'Name' exists in the hash table. |
In the above two code examples, we used the -contains operator and .Contains() method; both check if the key stored in $key exists in the $hashTable.
The -contains operator is a comparison operator that returns a Boolean value indicating whether the specified value exists in the collection of keys of the $hashTable or not. It compared each key in the collection one by one to the value stored in the variable $key and returned True if a match was found; otherwise, it would be False.
Some learners use the -eq operator instead of -contains, though the -eq operator may work sometime but using the -eq operator to check if the collection of keys in the hash table is equal to the string 'Name' does not make sense because a collection of strings cannot be equal to a single string.
It will be more appropriate to use the .Contains() method on the $hashTable.Keys collection or the -contains operator to check for a specific key in the hash table.
It is important to note that the
-containsoperator is case-insensitive, meaning the'Nameand'name'keys are the same. Also, the-containsoperator is faster than the.Contains()method when it comes to performance.
That’s all about how to check if HashTable contains key in PowerShell.