PowerShell Counting Objects 0 ▲ Clayton Errington 1 hour ago · Tech · hide · 0 comments Why I Replaced .Count with Measure-Object in My PowerShell Code. I tried the .Count and it kept returning 0 when I knew there was more than one in the count. This caused problems when I needed to count if there was great then 0 or 1 items, and .Count was unreliable. Moving to Measure-Object was, and this is why. Recently, I made a small change throughout some PowerShell code that looked like this: "CriticalIssues" = ($issues | Where-Object { $_.Severity -eq "Critical" }).Count and changed it to: "CriticalIssues" = ($issues | Where-Object { $_.Severity -eq "Critical" } | Measure-Object).Count At first glance, the change seems unnecessary. Both expressions typically return the same result. If there are five critical issues, both return 5. So why make the change? The answer comes down to understanding what .Count and Measure-Object are actually doing behind the scenes. The Illusion That They’re the Same Consider a simple example: $numbers = 1..5 $numbers.Count Output: 5 Now compare that… No comments yet. Log in to reply on the Fediverse. Comments will appear here.