如何在powershell哈希中查找重复值

1 powershell hash duplicates having

想象一下以下哈希:

$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')
Run Code Online (Sandbox Code Playgroud)

什么查询会返回2个重复值'a'和'c'?

基本上我正在寻找PowerShell等效的以下SQL查询(假设表h(c1,c2):

select c1
from h 
group by c1
having count(*) > 1 
Run Code Online (Sandbox Code Playgroud)

Fro*_* F. 7

你可以试试这个:

$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}
Run Code Online (Sandbox Code Playgroud)

如果存储结果,则可以深入到组中以获取重复条目的键名.防爆.

$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 
Run Code Online (Sandbox Code Playgroud)

  • 解决方案完美运行。日内瓦有免费啤酒等着你,只要你在附近就告诉我。 (2认同)
  • 如果这解决了您的问题,您可能希望接受它,因为问题仍然是开放/未答复的.:-) (2认同)