Search notes:

PowerShell cmdLet measure-object

measure-object returns an instance of System.Management.Automation.GenericMeasureInfo.

Determine simple statistics

4,6,3,7 | measure-object -minimum -maximum -average -sum
#
# Count             : 4
# Average           : 5
# Sum               : 20
# Maximum           : 7
# Minimum           : 3
# StandardDeviation :
# Property          :
Github repository about-PowerShell, path: /cmdlets/object/measure/int-array.ps1

Measuring statistics of multiple properties (emulate SQL group by)

The -property option allows to specify the properties for which a set of statistical values should be returned. Such a set somewhat corresponds to a record that is returned by an SQL group by operation:
 get-process | measure-object `
    -property workingSet,PagedMemorySize64,VirtualMemorySize64 `
    -minimum -maximum -average -sum
#
# Count             : 437
# Average           : 32798031.0846682
# Sum               : 14332739584
# Maximum           : 1850437632
# Minimum           : 8192
# StandardDeviation :
# Property          : WorkingSet
#
# Count             : 437
# Average           : 38320076.4485126
# Sum               : 16745873408
# Maximum           : 1188343808
# Minimum           : 57344
# StandardDeviation :
# Property          : PagedMemorySize64
#
# Count             : 437
# Average           : 1146566214653.66
# Sum               : 501049435803648
# Maximum           : 2238949216256
# Minimum           : 8192
# StandardDeviation :
# Property          : VirtualMemorySize64
Github repository about-PowerShell, path: /cmdlets/object/measure/property.ps1

Pre-calculate value to be returned

Pre calculation with a script block:
 get-process | measure-object            `
    -sum { $_.workingSet        / 1MB }, `
         { $_.PagedMemorySize64 / 1MB } 
#
# Count             : 433
# Average           :
# Sum               : 13530.765625
# Maximum           :
# Minimum           :
# StandardDeviation :
# Property          :  $_.workingSet        / 1MB
# 
# Count             : 433
# Average           :
# Sum               : 16070.26953125
# Maximum           :
# Minimum           :
# StandardDeviation :
# Property          :  $_.PagedMemorySize64 / 1MB
Github repository about-PowerShell, path: /cmdlets/object/measure/property-scriptBlock.ps1

Select maximum value of a computation

The following example selects the maximum length of the txt properties that are passed to the cmdlet.
This example does not work on PowerShell 5.1 or below.
[psCustomObject] @{ nm = 99; txt = "ninety-nine" }  ,
[psCustomObject] @{ nm = 42; txt = "hello, world"}  ,
[psCustomObject] @{ nm =  0; txt = "zero"        }  |
measure-object -property { $_.txt.length } -maximum |
select-object maximum
Github repository about-PowerShell, path: /cmdlets/object/measure/max-length.ps1

See also

Powershell command noun: object

Index