Check if HashTable Contains Key in PowerShell

Check if Hashtable contains key in PowerShell

Using ContainsKey() Method

To check if HashTable contains a key in PowerShell,Use the if statement with the .ContainsKey() method.

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:

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.

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.

Using the -contains Operator

To check if HashTable contains a key in PowerShell, use the if statement with the -contains operator.

Alternatively, we can use the .Contains() method to do the same.

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 -contains operator is case-insensitive, meaning the 'Name and 'name' keys are the same. Also, the -contains operator is faster than the .Contains() method when it comes to performance.

That’s all about how to check if HashTable contains key in PowerShell.

Was this post helpful?

Leave a Reply

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