1

Imagine the following hash:

$h=@{}
$h.Add(1,'a')
$h.Add(2,'b')
$h.Add(3,'c')
$h.Add(4,'d')
$h.Add(5,'a')
$h.Add(6,'c')

What query would return the 2 duplicate values 'a' and 'c' ?

Basically I am looking for the powershell equivalent of the following SQL query (assuming the table h(c1,c2):

select c1
from h 
group by c1
having count(*) > 1 

3 Answers 3

9

You could try this:

$h.GetEnumerator() | Group-Object Value | ? { $_.Count -gt 1 }

Count Name Group                                                                   
----- ---- -----                                                                   
    2 c    {System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}
    2 a    {System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}

If you store the results, you could dig into the group to get the key-name for the duplicate entries. Ex.

$a = $h.GetEnumerator() | Group-Object Value | ? { $_.Count -gt 1 }

#Check the first group(the one with 'c' as value)
$a[0].Group

Name Value
---- -----
6    c    
3    c 
Sign up to request clarification or add additional context in comments.

2 Comments

Solution working perfectly. There's free beer waiting for you in Geneva CH, let me know whenever you're in the vicinity.
If this solved your problem, you might want to accept it as the question is still open/unanswered. :-)
1

You can use another hash table:

$h=@{}
$h.Add(1,'a')
$h.Add(2,'b')
$h.Add(3,'c')
$h.Add(4,'d')
$h.Add(5,'a')
$h.Add(6,'c')

$h1=@{}
$h.GetEnumerator() | foreach { $h1[$_.Value] += @($_.name) }
$h1.GetEnumerator() | where { $_.value.count -gt 1}

Name                           Value                                                                                                  
----                           -----                                                                                                  
c                              {6, 3}                                                                                                 
a                              {5, 1}                                                                                                 

1 Comment

Same comment for you Mjolinor, your solution works as well and there's free beer for you too whenever you happen to be in my neck of the woods.
1

Just a slightly different question:

How to list the duplicate items of a PowerShell Array

But a similar solution as from Frode F:

$Duplicates = $Array | Group | ? {$_.Count -gt 1} | Select -ExpandProperty Name

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.