24/7/365 Support

Measure Statistical Properties of a List in Windows PowerShell

Problem

You want to measure the numeric (minimum, maximum, sum, average) or textual (characters, words, lines) features of a list of objects.

Solution

Use the MeasureObject cmdlet to measure these statistical properties of a list. To measure the numeric features of a stream of objects, pipe those objects to the MeasureObject cmdlet: PS >1..10 | MeasureObject –Average Sum

Count : 10 Average : 5.5 Sum : 55 Maximum : Minimum : Property :

To measure the numeric features of a specific property in a stream of objects, supply that property name to the –Property parameter of the MeasureObject cmdlet. For example, in a directory with files:

PS >GetChildItem | MeasureObject Property Length Max Min Average Sum

Count : 427 Average : 10617025.4918033 Sum : 4533469885 Maximum : 647129088 Minimum : 0 Property : Length

To measure the textual features of a stream of objects, use the –Character, Word, and –Line parameters of the MeasureObject cmdlet:

PS >GetChildItem > output.txt PS >GetContent output.txt | MeasureObject Character Word Line

Lines
Words
Characters Property

964
6083
33484

Discussion

By default, the MeasureObject cmdlet counts only the number of objects it receives. If you want to measure additional properties (such as the maximum, minimum, average, sum, characters, words, or lines) of those objects, then you need to specify them as options to the cmdlet.

For the numeric properties, though, you usually don’t want to measure the objects themselves. Instead, you probably want to measure a specific property from the list—such as the Length property of a file. For that purpose, the MeasureObject cmdlet supports the –Property parameter to which you provide the property you want to measure.

Sometimes, you might want to measure a property that isn’t a simple number—such as the LastWriteTime property of a file. Since the LastWriteTime property is a DateTime, you can’t determine its average immediately. However, if any property allows you to convert it to a number and back in a meaningful way (such as the Ticks property of a DateTime), then you can still compute its statistical properties. Example 62 shows how to get the average LastWriteTime from a list of files.

Example 62. Using the Ticks property of the DateTime class to determine the average LastWriteTime of a list of files

PS >## Get the LastWriteTime from each file PS >$times = dir | ForeachObject { $_.LastWriteTime }

PS >## Measure the average Ticks property of those LastWriteTime

Example 62. Using the Ticks property of the DateTime class to determine the average LastWriteTime of a list of files (continued)

PS >$results = $times | MeasureObject Ticks Average

PS >## Create a new DateTime out of the average Ticks PS >NewObject DateTime $results.Average

Sunday, June 11, 2006 6:45:01 AM

For more information about the MeasureObject cmdlet, type GetHelp MeasureObject.

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say