How to use the -Property parameter for the PowerShell Measure Object cmdlet?

Why

$a = GPS AcroRd32 | Measure $a.Count 

work when

 GPS AcroRd32 | Measure -Property Count 

not

The first example returns the value 2 that I want, an integer.

The second example returns this:

 Measure-Object : Property "Count" cannot be found in any object(s) input. At line:1 char:23 + GPS AcroRd32 | Measure <<<< -Property Count + CategoryInfo : InvalidArgument: (:) [Measure-Object], PSArgumentException + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCommand 



This script - style entry is where I found out how to use the "Count" property in the first code sample.

The second code example is really confusing. The following statement works in this Script Center link :

 Import-Csv c:\scripts\test.txt | Measure-Object score -ave -max -min 

It still works, even if it is rewritten like this:

 Import-Csv c:\scripts\test.txt | Measure-Object -ave -max -min -property score 

I don't have much trouble accepting this until I review the Measure-Object's Help page . The parameter definition for -Property <string[]> states:

By default, the Count (Length) property of the object is used.

If Count is the default, then the explicit Count count should not work

 GPS AcroRd32 | Measure -Property Count # Fails 

The following is the information I need, except that it does not provide me with an integer to perform operations, as you will see:

 PS C:\Users\Me> $a = GPS AcroRd32 | Measure PS C:\Users\Me> $a Count : 2 Average : Sum : Maximum : Minimum : Property : PS C:\Users\Me> $a -is [int] False 



So, why does Dot Notation ( $a.count ) work, but not an explicitly written statement ( GPS | Measure -Property Count )?

If I have to use Dot Notation, then I will, but I would like to take this opportunity to learn more about how and * why PowerShell works this way, and not just build a superficial understanding of PowerShell syntax. In other words, I want not to turn into a Handicraft Programmer / Code Monkey .

+6
source share
4 answers

One thing you need to know is that in PowerShell in general and especially in CmdLets you manage objects or a collection of objects.

Example: if only one "AcroRd32" is running, Get-Process will return [System.Diagnostics.Process] , if more than one is working, it will return the [System.Diagnostics.Process] collection.

In the second case, you can write:

 (GPS AcroRd32).count 

Because the collection has the count property. The collection of duality objects is also valid in the CmdLets parameters, which most of the time support objects or a list of objects (the collection is constructed using the, operator).

 PS C:\> (gps AcroRd32) -is [object[]] True 

Just use the Get-Member cmdlet:

 PS C:\> (gps AcroRd32) | Get-Member TypeName: System.Diagnostics.Process Name MemberType Definition ---- ---------- ---------- Handles AliasProperty Handles = Handlecount ... ... 

and

 PS C:\> Get-Member -InputObject (gps AcroRd32) TypeName: System.Object[] Name MemberType Definition ---- ---------- ---------- Count AliasProperty Count = Length ... ... 
+5
source

Because the COUNT property is a property of the OUTPUT object (i.e. the results of the Measure-Object ), not the INPUT object.

The -property parameter specifies which property of the input features should be evaluated. None of these are COUNT unless you pass in an array or arrays or something else.

+6
source

I think you want something like this:

 gps AcroRd32 | measure-object | select -expand Count 
+4
source

If you are just looking for a counter, you can do the following:

 $a = GPS AcroRd32 $a.Count = 2 

$a = GPS AcroRd32 sets $ a to an array of process objects. The array has a member, a graph that will allow you to determine the number of elements already.

The Measure-Object parameter (with the alias measure ) is used to measure the average, maximum, minimum, and total values โ€‹โ€‹of the property. So you can do something like $a | measure -property Handles -sum $a | measure -property Handles -sum and get the number of total number of open descriptors.

+2
source

Source: https://habr.com/ru/post/898402/


All Articles