Using the Measure-Object Cmdlet

10/22/2009

2 minutes to read

In this article

Calculating Basic Statistics

The Measure-Object cmdlet provides a way to quickly generate statistics (count, average, sum, minimum and maximum values) for an object. For example, suppose we have the following text file, which lists user names and their score on … well, something (it doesn’t matter what):

Suppose you’d like to calculate the number of scores, the average score, and the highest and lowest score. Believe it or not you can do all that with a single command:

Import-Csv c:\scripts\test.txt | Measure-Object score -ave -max -min

The command itself is pretty straightforward. We use the Import-Csv cmdlet to import data from the text file; that data is then piped to the Measure-Object cmdlet. On that side of the pipeline we call Measure-Object followed by the name of the property or item (in this case, score) that we want to generate statistics for. Notice, too that we include the -ave, -max, and -min parameters (leaving out-sum). That’s important: with the exception of count (the number of items in the dataset) you get back only those statistics specially included in the command.

Keep in mind that Measure-Object is used only for generating statistics; it doesn’t do things like show you the people who got, say, the five best scores. If you want that kind of information you need to sort the data and then use the Select-Object cmdlet, like so: