powershelltimespan

Arithmetic with PowerShell Timespans


PowerShell Timespans are great for quickly displaying durations, as in:

$starttime = $(get-date)
{ do some processing here }
write-host "Duration: $((new-timespan $starttime $(get-date)).tostring())"

But if I did $loopcount iterations of processing during that timeframe, how do I divide the duration by $loopcount to get the average duration per iteration?


Solution

  • A slightly nicer way if doing this (if you don't need output from your script block) would probably be:

    1..$loopcount | ForEach-Object {
        Measure-Command {
            # do some processing here
        }
    } | Measure-Object -Average TotalSeconds