Context

PowerShell 7 script with Set-StrictMode -Version Latest, summing a numeric property per group. A filter inside a group (for example “only the failing rows”) can leave an empty set.

Insight

On an empty input, Measure-Object -Sum does not give 0 or $null. Reading .Sum throws:

The property 'Sum' cannot be found on this object. Verify that the property exists.

A cast such as [int](...).Sum doesn’t help, because the property access throws before the cast runs. The code works in testing and fails only on the one group that happens to be empty, far from the real cause.

Terminal window
Set-StrictMode -Version Latest
(@() | Measure-Object -Property Count -Sum).Sum # throws

Fix

Sum with a plain loop, which returns 0 for an empty input:

Terminal window
function Get-Total {
param([AllowEmptyCollection()][object[]]$Rows)
$total = 0
foreach ($row in $Rows) { $total += $row.Count }
return $total
}

Treat -Minimum and -Maximum the same way, unless the input is known to be non-empty.