jamiet jamiet - 9 months ago 50
PowerShell Question

Understanding uniqueness in arrays of hashtables

Take this code, with output:

> (@(@{"number"=1},@{"number"=1},@{"number"=2})).Count
> (@(@{"number"=1},@{"number"=1},@{"number"=2}) | select -uniq).Count

I was expecting the second statement to return
. Clearly it doesn't but I don't understand the rationale for why not. Why does the
option consider all of these three hashtables to be the same?

A follow on question, I want to remove duplicates from this:


in order that the result is


How do I do that?


Use Sort-Object -Unique instead of Select-Object -Unique if you want to evaluate the uniqueness of values but have the entire hashtable returned:

@(@{"number"=1},@{"number"=1},@{"number"=2}) | Sort-Object Values -Unique