Checking for identical hashtable values

hashtable, powershell

Solution

Not the most effective way but a very simple is the `@($h.Values | Group-Object).Count -eq 1`.

Example:

# all the same
$h = @{ server1=0; server2=0; server3=0 }

# gets True
@($h.Values | Group-Object).Count -eq 1

# different
$h = @{ server1=0; server2=0; server3=1 }

# gets False
@($h.Values | Group-Object).Count -eq 1

The special case is how to treat a table with 0 items. E.g. if it means all the same then `-le 1` can be used in the check.

Problem

How do I loop through a hashtable to verify that all the values are the same? I have a script that returns a hashtable from an active-directory query. The value returned from each server should be the same so I want to return true if all values are the same or false if any values do not match the others. Ideally this would work even if a 4th or 5th server gets added without needing to update the script. ``` Name Value ---- ----- server1 0 server2 0 server3 0 ```

Original source