3.9. Gathering Collection Statistics

Commonly, when dealing with a big data set, we need to gather statistics that involve querying every object. This is where the Measure-Object cmdlet comes into play. It will go over a collection and calculate several of the most common statistics for a property value: count, sum, minimum, maximum, and average—with a minimal amount of code. As an example, let's collect statistics about all file sizes:

PS> dir -r | Measure-Object -property Length -min -max -average -sum


Count    : 2565
Average  : 55113.0101364522
Sum      : 141364871
Maximum  : 19622912
Minimum  : 0
Property : Length

The preceding query finds all files recursively by using the dir command's -r switch. It then loops over the result, queries each file's ...

Get Pro Windows PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.