How to get hash table key which has specific value?

powershell hashtable get value by key name
powershell search table for value
powershell find max value in hash table
powershell hashtable check if key exists
print out hash table powershell
powershell nested hash table
powershell lookup table
powershell hashtable getenumerator

I am having a hash table where Keys are being used based on value.

For ex.

    $ComponentTobeBuild=@{"ComponentNameX"="True";
                          "ComponentNameXyz"="False";
                          "SomeComponent"="False"}

I would like to get the keys which are having values True. (I will pass the key to some other script as parameter).

I was trying like that , But i think some where i am missing as it is not listing the keys.

$($ComponentToBuild.Keys) | Where-Object { $_.Value -eq "True" }

How to get the component Name which are having denoted as True? Also i would like to know whether hash table is a wise choice for this kind of work. Because I thought that Hash table will be mainly be used for processing the values.


Hi this should work for what you want.

$ComponentTobeBuild=@{"ComponentNameX"="Test";
                          "ComponentNameXyz"="False";
                          "SomeComponent"="False"}                    

Foreach ($Key in ($ComponentTobeBuild.GetEnumerator() | Where-Object {$_.Value -eq "Test"}))
{$Key.name}

C#, Or the property is set, key does not exist in the collection, and the Hashtable has a fixed size. Note: This property returns the value associated with the specific key. C# | Check if the Hashtable contains a specific Key The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsKey (Object) Method is used to check whether the Hashtable contains a specific key or not.


$ComponentTobeBuild.GetEnumerator() | ? { $_.Value -eq "True" }

C#, C# | How to get hash code for the specified key of a Hashtable · C# | Gets an ICollection containing the values in the Hashtable · Sahil_Bansall. Check out this​  How can I look up a specific value that is associated with a specific key in a Windows PowerShell hash table? Use the Item method and supply the key, for example: $hash = @ { 2 = "Removable disk"


I know this is old, but I saw this on my search for an other problem and wondered why you bother with Enumerators. Just build the code exactly as the question in real language:

Give me all keys, where the value is 'True'.

$ComponentTobeBuild.Keys | ? { $ComponentTobeBuild[$_] -eq 'True'  }

For sake of consistency I would encapsulate that in @() to get an Array even if there is just one or no result.

As for the Solution the asker had, the problem is that $_ is a String and .Value is not the value of the key. It simply doesn't exist in a String. You have to get the value of the hashtable with $_ as Key in order to compare it.

C# Hashtable, Checks whether the hashtable contains a specific value. GetHash, Returns the hash code for the specified key. Add key-value into Hashtable. The Add() method​  As we can see from the previous output, the Hashtable get method returns null if the specified key is not mapped to any value in the hash table object. If you want to get a default value instead of a null if the key does not exist in the hash table object, then you can use the Hashtable getOrDefault method.


The problem with filtering Hashtables in PowerShell is that you can't iterate over the Key-Value pairs.

Let's say you have some collection:

$collection = @{ 
    a = 'A'; 
    b = 'B'; 
    c = 'C'; 
    d = 'D'; 
    e = 'E';
    ...
}

You would expect to be able to use

$results = $collection | Where-Object { $_.Key -in ('a', 'b', 'd') }

in PowerShell, but that's not possible.

Therefore you have fall back to using the enumerator:

$results = $collection.GetEnumerator() | Where-Object { $_.Key -in ('a', 'b', 'd') }
$results

Name                           Value
----                           -----
d                              D
b                              B
a                              A

PowerShell – Hash Table Get That Value, On problem there is if you are working with hash tables is that you have a key value pair and you would like to retrieve a value for a specific key. Given I have the below clients hash, is there a quick ruby way (without having to write a multi-line script) to obtain the key given I want to match the client_id? E.g. E.g. How to get the key for client_id == "2180" ?


You could also use the Dictionary.Keys' Where method which returns all elements where a condition holds true. That condition could be a lambda asserting that the tested key $_ relates to a "True" value in the dictionary.

$ComponentTobeBuild=@{"ComponentNameX"="True";
                      "ComponentNameXyz"="False";
                      "SomeComponent"="False"}
$ComponentTobeBuild.Keys.Where({$ComponentTobeBuild[$_] -eq "True"})
# -> @("ComponentNameX")

about_Hash_Tables, The order of keys in a hash table is not determined. line:1 char:1 + [ordered]$​hash = @{} + [!INCLUDE[]()] The ordered attribute can be specified only on a hash literal node. Hash tables have Keys and Values properties. Keep in mind, in a Hash Table you have Keys, and Values. So, we are going to search for a ‘Key’ in order to get the ‘Value’ so we can extract the Month name. Here’s the Hash Table Syntax for finding the value using the key: $yourHashTable. Item([#key#]) 2.


Hashtable.Contains(Object) Method (System.Collections), Console.WriteLine("The Hashtable contains the following values:"); PrintIndexAndKeysAndValues(myHT); // Searches for a specific key. int myKey = 2; Console. It's not easy to get key from value in Hashtable or HashMap, as compared to getting value from key because HashMap or Hashtable doesn't enforce one to one mapping between key and value inside Map in Java. in fact, Map allows same value to be mapped to multiple keys inside HashMap, Hashtable or any other Map implementation.


How to get Key From Value in Hashtable, HashMap or , Java Hashtable, HashMap or any other Map implementation are unidirectional e.g. you can get value if you have key but not vice-versa. In this  Note that hashtables are optimized for looking up a value given a key. Going in the reverse direction will be much slower as the size of the hash table gets bigger. If you have a big hash table, and will be doing a lot of reverse look ups, you might want to consider keeping two hash tables, a normal one, and one with the keys and values inverted.


Hashtable (Java Platform SE 8 ), To successfully store and retrieve objects from a hashtable, the objects used as keys Returns the value to which the specified key is mapped, or null if this map​  C# | Check if the Hashtable contains a specific Value The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.ContainsValue (Object) Method is used to check whether the Hashtable contains a specific value or not.