Why do I get this output in PowerShell sort -unique Count

Question:

I typed the following command to find out how many unique objects there were and it gave me 5. I don’t know why this gives 5.

Answer:

$var | sort -Unique COUNT is the same as: $var | sort -Unique -Property COUNT

So what sort is doing is looking for the “COUNT” property on each of the elements in the array to determine whether they are unique or not. You can see how this works if you do the following:

Since none of the objects have a “COUNT” property, sort sees them all as the same and therefore none are unique and it is returning one of the elements. The clue came from trying the following:

this produced the result “c”.

Measure is your friend here:

That should do the trick.

Source:

Why do I get this output in PowerShell sort -unique Count by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply