Skip to content

Allow Measure-Object to accept ad-hoc script-block arguments in lieu of property names #6855

@mklement0

Description

@mklement0

This issue arose out of #6837.

Motivation:

Sort-Object's -Property not only supports property names (the typical case), but also script blocks that are evaluated for each input object and thereby enable sorting criteria derived from properties, which is a powerful feature (also found, in essence, in Select-Object); e.g.:

# Sort by only the numerical part of the input, via a script-block parameter argument.
PS> 'z1', 'a0', 'b10' | Sort-Object -Property { [int] $_.Substring(1) }
a0
z1
b10

Suggestion:

It would be handy if Measure-Object supported analogous use; e.g.:

# Count the  odd numbers in the input.
PS> 1, 2, 3 | Measure-Object -Sum -Property { $_ % 2 }  # wishful thinking
2

To that end:

  • The -Property parameter's data type would have to change from[string[]] to [object[]]
  • In the event of a script block getting passed, it would have to be evaluated for each input object and used in lieu of a simple property access on the input object.

Environment data

Written as of:

PowerShell Core v6.0.2

Metadata

Metadata

Assignees

Labels

Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions