Skip to content

Measure-Object should support TimeSpan #10712

@felixfbecker

Description

@felixfbecker

Summary of the new feature/enhancement

TimeSpans have all arithmetic operators defined on them (addition, subtraction, division, multiplication, equality, greater/less than). However, Measure-Object only works on numbers. If you pipe a TimeSpan to it, it will error with

measure-object : Input object "00:00:00" is not numeric.

It would be very useful if Measure-Object could work on TimeSpan objects too.
Example use case: Measure the time spent in meetings today and their average duration:

Get-GSCalendarEvent -TimeMin (Get-Date).Date -TimeMax (Get-Date).Date.AddDays(1) -SingleEvents |
  ForEach-Object { $_.End.DateTime - $_.Start.DateTime } |
  Measure-Object -AllStats

Count             : 3
Average           : 1:00:00
Sum               : 3:00:00
Maximum           : 2:00:00
Minimum           : 0:30:00
StandardDeviation : 0:30:00
Property          :

Currently, this is more cumbersome:

Get-GSCalendarEvent -TimeMin (Get-Date).Date -TimeMax (Get-Date).Date.AddDays(1) -SingleEvents |
  ForEach-Object { $_.End.DateTime - $_.Start.DateTime } |
  ForEach-Object { $s = New-TimeSpan } { $s += $_ } { $s }

And this only gets you the sum. All the other stats require way more code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    HacktoberfestPotential candidate to participate in HacktoberfestIssue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-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