PowerShell: How to Find Minimum and Maximum Values in Array


Often you may want to find the minimum and maximum values of an array in PowerShell.

You can use the following methods to do so:

Method 1: Find Minimum Value of Array

($my_array | Measure-Object -Minimum).Minimum

This particular example will return the minimum value in the array named $my_array.

Method 2: Find Maximum Value of Array

($my_array | Measure-Object -Maximum).Maximum

This particular example will return the maximum value in the array named $my_array.

The following examples show how to use both methods in practice.

Example 1: Find Minimum Value of Array in PowerShell

Suppose that we create the following array in PowerShell named $my_array:

$my_array = 4, 15, 20, 51, 30, 45, 50, 3, 16, 29, 22

We can use the following syntax to calculate the minimum value of this array:

($my_array | Measure-Object -Minimum).Minimum

The following screenshot shows how to use this syntax in practice:

PowerShell find minimum value of array

From the output we can see that the minimum value of the array is 3.

Example 2: Find Maximum Value of Array in PowerShell

Suppose that we create the following array in PowerShell named $my_array:

$my_array = 4, 15, 20, 51, 30, 45, 50, 3, 16, 29, 22

We can use the following syntax to calculate the maximum value of this array:

($my_array | Measure-Object -Maximum).Maximum

The following screenshot shows how to use this syntax in practice:

PowerShell find maximum value of array

From the output we can see that the maximum value of the array is 51.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Calculate the Average of an Array
PowerShell: How to Compare Two Arrays
PowerShell: How to Get First Item in Array
PowerShell: How to Find Duplicate Values in Array

Leave a Reply