Skip to content

Add a -Recurse switch to Measure-Object to support input objects / properties that are collections themselves #7244

@mklement0

Description

@mklement0

Currently, Measure-Object treats each input object / input object's property as a scalar; e.g.:

PS> ((1, 2), (3, 4) | Measure-Object).Count
2 # The input arrays each counted as *1* object - their elements weren't counted.

PS> ([pscustomobject] @{ prop = 1, 2 }, [pscustomobject] @{ prop = 3, 4 } | 
      Measure-Object -Property prop).Count
2  # The arrays stored in .prop each counted as *1* object - their elements weren't counted.

Note that if you also specified a switch such as -Sum, the commands would break, because an attempt is made to interpret the array as a number, which fails: Measure-Object : Input object "System.Object[]" is not numeric.

It would be handy if a -Recurse switch would allow opting into recursing into array-valued input objects / properties:

PS> ((1, 2), (3, 4) | Measure-Object -Recurse).Count  # WISHFUL THINKING
4   # The input arrays' elements were counted individually

PS> ([pscustomobject] @{ prop = 1, 2 }, [pscustomobject] @{ prop = 3, 4 } | 
      Measure-Object -Recurse -Property prop).Count  # WISHFUL THINKING
4 # The elements of the arrays stored in .prop were counted individually

Environment data

Written as of:

PowerShell Core v6.1.0-preview.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions