Finding Minimum and Maximum Values

by Sep 5, 2014

All PowerShell Versions

To find the smallest and largest item in a range of numbers, use Measure-Object:

$list = 1,4,3,1,3,12,990

$result = $list | Measure-Object -Minimum -Maximum
$result.Minimum
$result.Maximum

This works for any input data and any data type. Here is a slight modification that returns the oldest and newest file in your Windows folder:

$list = Get-ChildItem -Path C:\windows 

$result = $list | Measure-Object -Property LastWriteTime -Minimum -Maximum
$result.Minimum
$result.Maximum

Simply add the –Property parameter if your input data has multiple properties, and pick the one you want to examine.

Twitter This Tip! ReTweet this Tip!