How to Find the Highest Number in an Array in PowerShell?

Today, I got a task to find the highest number in a PowerShell array. I used a few methods to achieve this. In this PowerShell tutorial, we’ll explore how to find the largest number in a PowerShell array using various methods and examples.

To find the highest number in an array in PowerShell, use the Measure-Object cmdlet with the -Maximum parameter. For example, $maxValue = ($array | Measure-Object -Maximum).Maximum will return the largest number in the array $array. This command calculates the maximum value of the elements within the array.

Find the Highest Number in an Array in PowerShell

Let us say, here is I have an array in PowerShell which is having a few numbers as elements or items.

$numbers = 8, 3, 11, 6, 4

This array $numbers contains five integers. I want to find the largest number from this array in PowerShell using the below 3 methods.

1. Using Measure-Object

The simplest way to find the largest number in an array in PowerShell is by using the Measure-Object cmdlet. This cmdlet can measure various properties of objects, including numbers in an array. Here’s a basic example:

$numbers = 8, 3, 11, 6, 4
$highestNumber = ($numbers | Measure-Object -Maximum).Maximum
Write-Host "The highest number is $highestNumber"

In this example, Measure-Object calculates the maximum value of the numbers in the $numbers array, and we access that value through the Maximum property.

Once you execute the PowerShell script, you can see the output like in the screenshot below:

Find the Highest Number in an Array in PowerShell

2. Using the Sort-Object Cmdlet

Another method to find the largest number is by sorting the array in PowerShell. You can use the Sort-Object cmdlet to sort the array and then select the last item, which will be the highest number:

Here is a complete PowerShell script to find the highest number in an array in PowerShell using the sort-object method.

$numbers = 8, 3, 15, 6, 4
$sortedNumbers = $numbers | Sort-Object
$highestNumber = $sortedNumbers[-1]
Write-Host "The highest number is $highestNumber"

Here, $sortedNumbers[-1] retrieves the last item from the sorted array, which is the highest number.

Check out the screenshot below, you can see the output, I executed the script using VS code, you can use any editor like PowerShell ISE also.

How to Find the Highest Number in an Array in PowerShell

3. Using Foreach Loop

If you want to understand the logic behind finding the maximum value, you can use a foreach loop. This method iterates through each number and compares it with a variable that holds the maximum value found so far.

This is also another method in PowerShell to get the highest number from a PowerShell array.

$numbers = 8, 3, 11, 6, 4
$highestNumber = $numbers[0]

foreach ($number in $numbers) {
    if ($number -gt $highestNumber) {
        $highestNumber = $number
    }
}

Write-Host "The highest number is $highestNumber"

In this code, -gt stands for “greater than.” We start by assuming the first number is the highest, and as we loop through the array, we update $highestNumber if we find a larger one.

4. Using the [Linq] Namespace

For those who are a bit more advanced and are familiar with .NET, you can use the LINQ (Language-Integrated Query) to find the highest number in an array:

$numbers = 8, 3, 11, 6, 4
$highestNumber = [Linq.Enumerable]::Max($numbers)
Write-Host "The highest number is $highestNumber"

This method uses the Max method from the LINQ Enumerable class to find the highest number.

Conclusion

You can find the highest number in an array in PowerShell using various methods like:

  • Using Measure-Object
  • Using the Sort-Object Cmdlet
  • Using Foreach Loop
  • Using the [Linq] Namespace

In this PowerShell tutorial, I have explained with examples, how to find the highest number in an array in PowerShell with various examples.

You may also like: