Megosztás a következőn keresztül:


Using the Measure-Object Cmdlet

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

Name,Score
Dan K. Bacon Jr.,64
Jean Philippe Bagel,89
Erzsébet Balázs,45
Martin Bankov,64
Peter Bankov,33
Angel Barbariol a,89
David Barber,88
Rob Barker,55
Miklós Barkóczi,86
Dave Barnett,56
Josh Barnhill,75
Adam Barr,33
Paula Barreto de Mattos,89
Gytis M. Barzdukas,62
Shai Bassli,87
Pilar Colome Bassols,24
Tomasz Bator,67
Shaun Beasley,77
Mark Bebbington,35
Anna Bedecs,78
Parry Bedi,86
Ann Beebe,64
Dick Beekman,74
Balázs Belinszki,79
Kostadin Belishky,55
Mason Bendixen,64
Almudena Benito,33
Ido Ben-Sachar,56
Wanida Benshoof,81

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.

Here’s what the returned data looks like:

Count    : 29
Average  : 65.1034482758621
Sum      :
Maximum  : 89
Minimum  : 24
Property : Score

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:

Import-Csv c:\scripts\test.txt | Sort-Object score -ascending | Select-Object -first 5

Here’s what that command returns:

Name                                    Score
----                                    -----
Paula Barreto de Mattos                 89
Angel Barbariol a                       89
Jean Philippe Bagel                     89
David Barber                            88
Shai Bassli                             87

And, yes, you can determine the five lowest scores: just use the exact same command but sort in ascending order (the default) rather than descending order:

Import-Csv c:\scripts\test.txt | Sort-Object score | Select-Object -first 5

Maybe this was a golf tournament, in which case these five people actually did quite well:

Name                                    Score
----                                    -----
Pilar Colome Bassols                    24
Peter Bankov                            33
Almudena Benito                         33
Adam Barr                               33
Mark Bebbington                         35