header image

Counting members

Posted by: | May 31, 2016 Comments Off on Counting members |

If you have a collection of objects

$proc = get-process

 

you can get the number of members using the Length property

PS>  $proc.Length
71

$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.

under: PowerShell Basics

Comments are closed.

Categories