Home > Software design >  How do I identify hashtable keys with multiple values in powershell
How do I identify hashtable keys with multiple values in powershell

Time:06-01

I have a hashtable created from an array like this:

$employeesHashtable = $employees | Group-Object -Property Initials -AsHashTable

How do I find keys having multiple values?

CodePudding user response:

The alternative to using .GetEnumerator() can be using the hash table Keys, the key collection implements ICollection and can be enumerated without issues:

$keysWithMultipleValues = $employeesHashtable.Keys.where{ $employeesHashtable[$_].Count -gt 1 }

CodePudding user response:

I ended up with this:

$keysWithMultipleValues = $employeesHashtable.GetEnumerator() | `
  ForEach-Object { [PSCustomObject]@{ Key = $_.Key; Count = $_.Value.Count } } | `
  Where-Object { $_.Count -gt 1 }
  • Related