0

I've got this Hashtable with this values:

Name                           Value                                                                                                            
----                           -----                                                                                                            
Bayas_palm_stem_A0311.jpg      1                                                                                                                
Bayas_palm_stem_A0312.jpg      2                                                                                                                
Bukit_Bangkong_area.tiff       1                                                                                                                
BY_and_siblings_A0259.jpg      5                                                                                                                
Cassava_camp_A0275.jpg         1                                                                                                                
Children_A0115.jpg             6                                                                                                                
cip_barau_kubak.jpg            1

How can i get only the Name and Value where Value is greater than 1 ?

I'm tryng with this code, but i'm doing something wrong!!!

$RT | Group-Object Name , Value | Where-Object {$RT.Values -gt 1} 

Thanks a lot for any help.

3 Answers 3

3

Use $hashtable.GetEnumerator() to enumerate the individual name-value pairs in the hashtable:

$RT.GetEnumerator() |Where-Object {$_.Value -gt 1}

Beware that if you assign the resulting pairs to a variable, it's not longer a hashtable - it's just an array of individual name-value pairs.

To create a new hashtable with only the name-value pairs that filter through, do:

$RTFiltered = @{}
$RT.GetEnumerator() |Where-Object {$_.Value -gt 1} |ForEach-Object {$RTFiltered.Add($_.Name, $_.Value)}
Sign up to request clarification or add additional context in comments.

Comments

1

Like this:

$RT.getenumerator() | Where-Object {$_.Value -gt 1}

Comments

1

Assuming you have a Hashtable defined like this.

$rt = @{
"Bayas_palm_stem_A0311.jpg " =     1                                                                                                             
"Bayas_palm_stem_A0312.jpg " =     2                                                                                                               
"Bukit_Bangkong_area.tiff  " =     1                                                                                                              
"BY_and_siblings_A0259.jpg " =     5                                                                                                             
"Cassava_camp_A0275.jpg    " =     1                                                                                                            
"Children_A0115.jpg        " =     6
"cip_barau_kubak.jpg       " =     1
}

You can call GetEnumerator() which allows you to iterate through the Hashtable.

Once you've got an enumeration of the members, then your normal PowerShell value comparisons will work.

You can get values greater than 1 like this:

$rt.GetEnumerator() | ? Value -gt 1

Name                           Value                                                                                            
----                           -----                                                                                            
BY_and_siblings_A0259.jpg      5                                                                                                
Children_A0115.jpg             6                                                                                                
Bayas_palm_stem_A0312.jpg      2   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.