Monthly Archive

Categories

Measure-Object Enhancement

PowerShell v6.1 preview 4 brings a Measure-Object enhancement.

Measure-Object has been around since PowerShell v1. It enables you to measure the properties of an object and easily perform a number of calculations.

PS> Get-ChildItem -File | Measure-Object -Property Length

Count : 6
Average :
Sum :
Maximum :
Minimum :
StandardDeviation :
Property : Length

 

The original calculations were – Count, Sum, Average, Minimum and Maximum

Using Measure-Object was a very successful short cut in a number of solutions to scripting games puzzles over the years and was often the differentiator of those who really knew how to use PowerShell.

PS> Get-ChildItem -File | Measure-Object -Property Length -Average -Sum

Count : 6
Average : 2580291
Sum : 15481746
Maximum :
Minimum :
StandardDeviation :
Property : Length

 

PowerShell v6 added the calculation to produce the standard deviation

PS> Get-ChildItem -File | Measure-Object -Property Length -Average -StandardDeviation

Count : 6
Average : 2580291
Sum :
Maximum :
Minimum :
StandardDeviation : 5637125.25690079
Property : Length

 

PowerShell v6.1 preview 4 adds the –Allstats parameter so that you get get all of the calculations done in one pass

PS> Get-ChildItem -File | Measure-Object -Property Length -AllStats

Count : 6
Average : 2580291
Sum : 15481746
Maximum : 14027010
Minimum : 0
StandardDeviation : 5637125.25690079
Property : Length

 

A very useful addition to a very useful cmdlet.

Comments are closed.