0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Find Smallest Number in Array

Use $array | Measure-Object -Minimum | Select-Object -ExpandProperty Minimum to find the smallest number in an array, or use $array | Sort-Object | Select-Object -First 1 to get the smallest element.
📋

Examples

Input[5, 3, 9, 1, 7]
Output1
Input[10, 20, 30]
Output10
Input[-5, -10, 0, 2]
Output-10
🧠

How to Think About It

To find the smallest number in an array, look at each number and keep track of the smallest one you have seen so far. At the end, the smallest tracked number is the answer.
📐

Algorithm

1
Get the array of numbers.
2
Start with the first number as the smallest.
3
Compare each number in the array to the current smallest.
4
If a number is smaller, update the smallest number.
5
After checking all numbers, return the smallest number.
💻

Code

powershell
$array = @(5, 3, 9, 1, 7)
$smallest = $array | Measure-Object -Minimum | Select-Object -ExpandProperty Minimum
Write-Output "Smallest number is $smallest"
Output
Smallest number is 1
🔍

Dry Run

Let's trace the array [5, 3, 9, 1, 7] through the code.

1

Initial array

Array is 5, 3, 9, 1, 7

2

Find minimum

Measure-Object finds minimum value 1

3

Output result

Prints 'Smallest number is 1'

Array ValuesMinimum Found
5, 3, 9, 1, 71
💡

Why This Works

Step 1: Using Measure-Object

The Measure-Object -Minimum command scans the array and finds the smallest number.

Step 2: Extracting the value

We use Select-Object -ExpandProperty Minimum to get the number itself, not the object.

Step 3: Output the result

Finally, Write-Output prints the smallest number to the console.

🔄

Alternative Approaches

Sort and select first
powershell
$array = @(5, 3, 9, 1, 7)
$smallest = $array | Sort-Object | Select-Object -First 1
Write-Output "Smallest number is $smallest"
This sorts the entire array which can be slower for large arrays but is easy to understand.
Manual loop comparison
powershell
$array = @(5, 3, 9, 1, 7)
$smallest = $array[0]
foreach ($num in $array) {
  if ($num -lt $smallest) { $smallest = $num }
}
Write-Output "Smallest number is $smallest"
This uses a loop to find the smallest number manually, useful for learning basic logic.

Complexity: O(n) time, O(1) space

Time Complexity

The script checks each element once, so time grows linearly with array size.

Space Complexity

No extra arrays are created; it uses constant extra space.

Which Approach is Fastest?

Using Measure-Object is efficient and concise; manual loops are similar in speed but more verbose; sorting is slower due to full array sorting.

ApproachTimeSpaceBest For
Measure-Object -MinimumO(n)O(1)Quick and simple for any array size
Sort and select firstO(n log n)O(n)When sorted array is also needed
Manual loopO(n)O(1)Learning basic logic and control flow
💡
Use Measure-Object -Minimum | Select-Object -ExpandProperty Minimum for a quick and efficient way to find the smallest number.
⚠️
Forgetting to extract the minimum property with Select-Object -ExpandProperty Minimum results in an object, not the number.