Measure-Object helps you quickly find basic statistics like count, sum, average, minimum, and maximum from a list of numbers.
0
0
Measure-Object for statistics in PowerShell
Introduction
You want to know how many items are in a list.
You need the total or sum of values, like sales numbers.
You want to find the average score from test results.
You want to find the smallest or largest number in a dataset.
You want a quick summary of numeric data without writing complex code.
Syntax
PowerShell
Measure-Object [-Property <string>] [-Sum] [-Average] [-Minimum] [-Maximum] [-Line] [-Word] [-Character]
You can use -Property to specify which property of objects to measure.
Use flags like -Sum or -Average to get specific statistics.
Examples
Calculates the sum and average of the numbers 1 to 5.
PowerShell
1,2,3,4,5 | Measure-Object -Sum -Average
Finds the smallest and largest numbers in the array.
PowerShell
@(10, 20, 30) | Measure-Object -Minimum -Maximum
Counts lines, words, and characters in a text file.
PowerShell
Get-Content file.txt | Measure-Object -Line -Word -Character
Sample Program
This script measures the sum, average, minimum, and maximum of the numbers in the array and shows the results clearly.
PowerShell
$numbers = 5, 10, 15, 20, 25 $result = $numbers | Measure-Object -Sum -Average -Minimum -Maximum $result | Format-List
OutputSuccess
Important Notes
If you don't specify -Property, Measure-Object works on the input objects directly.
Measure-Object outputs an object with properties you can use in scripts.
For text files, use -Line, -Word, and -Character to get counts.
Summary
Measure-Object quickly gives basic statistics from data.
Use flags like -Sum, -Average, -Minimum, and -Maximum to get specific numbers.
It works on numbers, text lines, and object properties.