PowerShell for Windows Admins

May 31 2016   3:19PM GMT

Counting members

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


If you have a collection of objects

$proc = get-process

you can get the number of members using the Length property

PS>  $proc.Length

$proc is of type System.Array

PS>  $proc.GetType()

IsPublic IsSerial Name                                     BaseType
——– ——– —-                                     ——–
True     True     Object[]                                 System.Array

MSDN documenattion is at https://msdn.microsoft.com/en-us/library/system.array(v=vs.110).aspx

PowerShell also adds a Count property that is an alias of Length

if you are after the number of members in a collection via the pipeline you need to use the Measure-Object cmdlet

PS>  $proc | Measure-Object
Count    : 71
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

or measure directly

PS>  Get-Process | Measure-Object
Count    : 71
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

Notice that Measure-Object can also be used to discover other statistics – the Average, Sum, Maximum and Minimum. These only work for numeric properties.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: