O'Reilly logo

Pro Windows PowerShell by Hristo Deshev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required