Measure-Object
Use Measure-Object to count objects, or calculate sum, average, minimum and maximum for a numeric property.
Documentation: Microsoft Learn — Measure-Object
Syntax
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- Stage 1Get-Service
Positional values or no named parameters - Stage 2Measure-Object
Positional values or no named parameters - Stage 3Select-Object
Positional values or no named parameters
Try it
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.
Follow the objects
Run the command to inspect each stage.
Examples
Select Count
Show reference output
Illustrative output · values vary by environment
Count ----- 7
Get-ChildItem -Property Length
Show reference output
Illustrative output · values vary by environment
Count Property Sum Average Maximum ----- -------- -------- ------- -------- 4 Length 39323648 9830912 26214400
Select 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
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.
Put this command to work
Count matching objects
Count stopped services with Measure-Object; return the measurement object.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.
Inspect starting data
Supported syntax
Virtual files
Recap
Measure-Object Use Measure-Object to count objects, or calculate sum, average, minimum and maximum for a numeric property.
Next: Group-Object