I have 3 values stored in the variables a,b and c. I want to find the greatest among them. The values of the variables are: a = 3, b = 6, and c = 2. The result should be the name of the variable that contains the maximum value—the text b.
1 Answer
$a = 3
$b = 6
$c = 2
(Get-Variable -name a,b,c | Sort Value | Select -Last 1).Name
Result: b
This uses Get-Variable to return the variables $a, $b and $c, sorts the result by their value and then selects the last 1 from that sorted list (which as a result has the highest value) and returns it's Name property.