PowerShell Genie

Measure-Object

Use Measure-Object to count objects, or calculate sum, average, minimum and maximum for a numeric property.

Command 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Measure-Object

Syntax

1PS> Measure-Object [[-Property] <pspropertyexpression[]>] [-Sum] [-Average] [-Maximum] [-Minimum] [-StandardDeviation] [-Line] [-Word] [-Character] [-InputObject <PSObject>] [<CommonParameters>]
See the command anatomy

Inspect this authored example with fictional data in the bounded browser model. The numbered stages show how its objects change.

Get-Service | Measure-Object | Select-Object Count
  1. Stage 1Get-ServicePositional values or no named parameters
  2. Stage 2Measure-ObjectPositional values or no named parameters
  3. Stage 3Select-ObjectPositional values or no named parameters

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Select Count

    1PS> Get-Service | Measure-Object | Select-Object Count
    Show reference output

    Illustrative output · values vary by environment

    Count
    -----
    7    

    Get-ChildItem -Property Length

    1PS> Get-ChildItem 'C:\Scripts' | Measure-Object -Property Length -Sum -Average -Maximum
    Show reference output

    Illustrative output · values vary by environment

    Count  Property  Sum       Average  Maximum 
    -----  --------  --------  -------  --------
    4      Length    39323648  9830912  26214400

    Select Count, Sum

    1PS> Get-Process | Measure-Object -Property WorkingSet -Sum | Select-Object Count, Sum
    Show reference output

    Illustrative output · values vary by environment

    Count  Sum      
    -----  ---------
    5      831520768

    Parameters

    Parameter definitions:

    • -Property
    • -Sum
    • -Average
    • -Maximum
    • -Minimum
    • -StandardDeviation
    • -Line
    • -Word
    • -Character
    • -InputObject

    Measure-Object on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Measure-Object when you need to count objects, or calculate sum, average, minimum and maximum for a numeric property. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: A failed sum may indicate non-numeric values in the selected property. Inspect those records first, choose the intended numeric field and decide how missing values should affect the calculation. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: ForEach-Object. Measure-Object calculates supported aggregates; ForEach-Object processes each input object with a script block. A count or sum is different from returning one transformed result per input. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    Measure-Object reference on Microsoft Learn

    Put this command to work

    Count matching objects

    Count stopped services with Measure-Object; return the measurement object.

    Review the lesson

    Recap

    Measure-Object Use Measure-Object to count objects, or calculate sum, average, minimum and maximum for a numeric property.

    Next: Group-Object
    Genie guideMeasure-Object

    Use Measure-Object to count objects, or calculate sum, average, minimum and maximum for a numeric property. The console below is pre-loaded — run it, then change a value and run it again.