Measure-Object in PowerShell [With Examples]

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-Object

This 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:

Measure-Object in PowerShell

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-Object

Or, to count files in a folder:

Get-ChildItem 'C:\Logs' | Measure-Object

The 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 -Average

This measures string length and helps find the longest/shortest string in a data set—great for text analytics and validation.

Read PowerShell Write-Host

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 -Minimum

The 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).Count

This 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 -Maximum

The 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:

Measure-Object PowerShell

Read PowerShell Get-ChildItem

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 -Minimum

This 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 -Character

Useful 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 -IgnoreWhiteSpace

This 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 -Average

Calculates 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 -Average

Gives instant insight into datasets (e.g., employee years of service), supporting quick validation or reporting of business data.

Read PowerShell Where-Object

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 / 1MB

This allows easy embedding of statistical data in reports or dashboards.

Common Mistakes and Tips

  • Always specify -Property when measuring object properties other than the default.
  • Use [array] when necessary to enforce a collection before Measure-Object, especially for singular results.
  • Remember: The .Count property works on arrays, but Measure-Object always provides the Count field, 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/.Length can be faster for basic counts, but lack statistical summaries. For anything more than a simple count, Measure-Object is preferred.
  • Combine with Select-Object to pick specific properties, or use Group-Object for 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:

Leave a Comment

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.