August 12, 2014

Don’t Count, Measure

How often do you use the  Count property of an object to get the number of items in it ?

$SomeArray.count

The problem is that if you try to get the  Count property on a null object or on a object that is not a collection or an array it will throw an exception.

The solution is to use the Measure-Object cmdlet :

($SomeArray | measure-object).count

No comments:

Post a Comment