Get HashTable Max Value
@XCUD had another interesting chat the other day related to Hash Tables. He was going to blog about it, but he’s busy working on some very cool features for the next version of PowerWF.
Anyway, the question was, what is the easiest way to find the max value in a Hash table. We decided it was easiest to sort the hash table and then return the first value. Here is an example (albeit a silly one)
# Easiest way to get the max value from a hash table #
function get-HashMaxValue($hashTable)
{
return $hashTable.GetEnumerator() | sort value -Descending | select -First 1
}
# Let's try it out
$myHash = @{}
# Populate a hash table
get-Process c* | foreach-object -Process {$myHash.add($_.Name + $_.Id, $_.CPU)}
# Output the results
$myHash
# And call our function...
write-Output "`n`nAnd the Winner is..."
get-HashMaxValue $myHash
5 months ago - link

