---
title: "Measure-Object -Sum over an empty pipeline throws under StrictMode"
url: "https://www.dariuszparys.com/notes/measure-object-sum-empty-pipeline-strictmode"
author: "Dariusz Parys"
published: 2026-11-26T00:00:00.000Z
description: "With Set-StrictMode -Version Latest, Measure-Object -Sum returns nothing for an empty input, so reading .Sum throws a property-not-found error instead of giving 0."
tags:
  - "PowerShell"
  - "Shell"
---

# Measure-Object -Sum over an empty pipeline throws under StrictMode

## 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:

```text
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.

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

## Fix

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

```powershell
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.
