PowerShell’s Measure-Object cmdlet is a powerhouse for quickly analyzing data—be it counting items, calculating sums and averages, or measuring the length of strings and text files. Whether automating reporting or data aggregation tasks, mastering this cmdlet brings clarity and conciseness to scripts.
What is Measure-Object in PowerShell?
Measure-Object is a built-in cmdlet used to calculate numeric and statistical properties of pipeline input. It operates on arrays, objects, and even text, enabling users to measure counts, sums, averages, minimums, maximums, as well as word, line, and character counts in strings or files.
Basic syntax:
Measure-Object [-Property <string>] [-Sum] [-Average] [-Minimum] [-Maximum] [<Object[]>]Example:
1..10 | Measure-ObjectThis command counts the numbers (1 to 10) and returns the total, helping with instant data aggregation.
You can see the exact output in the screenshot below:

Check out PowerShell Start-Process
Basic Usage of Measure-Object
Here are some usages of the PowerShell Measure-Object cmdlet.
Counting Items
To count the total number of items:
1..50 | Measure-ObjectOr, to count files in a folder:
Get-ChildItem 'C:\Logs' | Measure-ObjectThe output’s Count property shows the total number of objects processed—critical for loops, reporting, or validation scripts.
Measuring String Length
Process string objects for insight into content length:
"PowerShell" | Measure-Object -Property Length -Maximum -Minimum -AverageThis measures string length and helps find the longest/shortest string in a data set—great for text analytics and validation.
Working with Files and Directories
Measuring File Sizes
To analyze sizes of files in a directory:
Get-ChildItem "C:\Logs" | Measure-Object -Property Length -Sum -Average -Maximum -MinimumThe Length property (bytes) enables calculation of total, largest, smallest, and average file sizes. For a more readable unit, convert bytes to MB or GB in further calculations.
Counting Files and Subfolders
Count everything inside a directory recursively:
(Get-ChildItem "C:\Projects" -Recurse | Measure-Object).CountThis is essential for directory analysis and for automation tasks such as cleanup scripts or audits.
Check out PowerShell Copy-Item
Measuring Numbers
Sum, Average, Minimum, Maximum
Measure numerical collections directly from the pipeline:
10, 20, 30, 40, 50 | Measure-Object -Sum -Average -Minimum -MaximumThe output supplies all statistical properties, making it easy to summarize datasets without resorting to loops or manual math.
You can see the exact output in the screenshot below:

Working with Object Properties
Aggregate numeric properties from objects. For example, analyzing memory usage across system processes:
Get-Process | Measure-Object -Property WorkingSet -Sum -Average -Maximum -MinimumThis snippet quickly summarizes RAM usage across all running processes—useful for system monitoring and troubleshooting.
Text Inputs and String Analytics
Count lines, words, and characters in a file:
Get-Content "C:\Temp\Report.txt" | Measure-Object -Line -Word -CharacterUseful in log analysis, documentation processing, or content stats, the output provides a full breakdown of the text’s structure.
Ignore whitespace in character counts:
Get-Content "C:\Temp\Report.txt" | Measure-Object -Line -Word -Character -IgnoreWhiteSpaceThis is ideal for normalized text measurement tasks.
Check out PowerShell Select-Object
Advanced Use Cases
Filtering Data Before Measuring
Combine Measure-Object with Where-Object to focus on targeted data:
Get-Process | Where-Object { $_.CPU -gt 10 } | Measure-Object -Property CPU -AverageCalculates average CPU usage—but only of processes exceeding a CPU threshold. This technique adds power to monitoring and reporting routines.
Calculating with ScriptBlock Properties
Use script blocks (PowerShell 6+) to customize calculations:
Get-ChildItem | Measure-Object -Sum { $_.Length / 1MB }This example totals file sizes but reports them in megabytes right from the pipeline—a huge efficiency boost in custom reporting.
Handling Data from CSVs
Measuring values from CSV files using the below PowerShell cmdlet:
Import-Csv D:\test\serviceyrs.csv | Measure-Object -Property Years -Minimum -Maximum -AverageGives instant insight into datasets (e.g., employee years of service), supporting quick validation or reporting of business data.
Output and Reporting
Understanding the Output
The main properties in a Measure-Object result can include:
- Count: Number of objects processed
- Sum: Total of selected property values
- Average: Mean value
- Minimum/Maximum: Extremes within the set
Reporting routines or scripts often pipe Measure-Object output into other formatters or files for further action.
Formatting for Reports
Store measurement results in variables for later use:
$result = Get-ChildItem "C:\Logs" | Measure-Object -Property Length -Sum
$MB = $result.Sum / 1MBThis allows easy embedding of statistical data in reports or dashboards.
Common Mistakes and Tips
- Always specify
-Propertywhen measuring object properties other than the default. - Use
[array]when necessary to enforce a collection beforeMeasure-Object, especially for singular results. - Remember: The
.Countproperty works on arrays, butMeasure-Objectalways provides theCountfield, so prefer it for script reliability. - Ensure correct property names—wrong or missing property names can yield empty or incorrect results.
Real-World Use Cases
- System monitoring: Summing all process memory, averaging CPU load, or counting log entries in real-time.
- File management: Auditing directory sizes, counting files for backup scripts, or identifying the largest files for cleanup.
- Text analytics: Calculating document or log file statistics for reporting or data quality checks.
Alternatives & Best Practices
.Count/.Lengthcan be faster for basic counts, but lack statistical summaries. For anything more than a simple count,Measure-Objectis preferred.- Combine with
Select-Objectto pick specific properties, or useGroup-Objectfor even richer reporting.
Conclusion
Measure-Object supercharges everyday PowerShell automation—handling everything from basic item counts and text analytics to advanced object property summarization. Its flexibility shines for file analysis, system reporting, and log management, making it a must-have tool for IT professionals and scripters alike.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.