How to use the "-Property" parameter for PowerShell's "Measure-Object" cmdlet?
Solution 1
One thing you need to know is that in PowerShell generally, and particulary in CmdLets you manipulate objects or collection of objects.
Example: if only one 'AcroRd32' is running Get-Process
will return a [System.Diagnostics.Process]
, if more than one are running it will return a collection of [System.Diagnostics.Process]
.
In the second case you can write:
(GPS AcroRd32).count
Because a collection has a count property. The duality object collection is also valid in CmdLets parameters that most of the time supports objects or list of objects (collection built with 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
... ...
Solution 2
Because the COUNT
property is a property of the OUTPUT object (i.e. results of Measure-Object
), not the INPUT object.
The -property
parameter specifies which property(ies) of the input objects are to be evaluated. None of these is COUNT
unless you pass an array or arrays or something.
Solution 3
I think what you want is something like this:
gps AcroRd32 | measure-object | select -expand Count
Solution 4
If you're just looking for the count 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 call, Count, that will allow you to determine the number of elements already.
The Measure-Object commandlet (with alias measure
) is used to measure the average, maximum, minimum, and sum values of a property. So you could do something like $a | measure -property Handles -sum
and get a count of the total number of open handles.
Stisfa
Super-Green-n00b programmer; to be more accurate, I'm coming from a Tech Support background (translation: VBS-conditioned, and yes, I can acknowledge that my mind has been mutilated by BASIC, but I don't regret it entirely). Trying to make an earnest effort to transition to Python, especially since it has so much good press (StackExchange community & otherwise). That and 2 extensions have caught my eye: py2exe & pywin32. The Django Framework has me pretty excited about web development, especially since PHP is pretty scary to me (ironically, I'm also trying to work with Wordpress, lol - no, I'm not bashing PHP, just that my own inadequacies as a programmer prevent me from embracing another language). Got a decent handle on HTML/XHTML & CSS, but they're not really programming, so that further attests to my n00b status. Hoping to eventually get into Web Development as a profession within this year, but if not, Walmart is always hiring =D (and there's always next year). Thanks to everyone who's helped me previously and thanks in advance to all those that will have to tolerate this neophyte.
Updated on June 12, 2022Comments
-
Stisfa almost 2 years
Why does
$a = GPS AcroRd32 | Measure $a.Count
work, when
GPS AcroRd32 | Measure -Property Count
doesn't?
The first example returns a value of
2
, which is what 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 Scripting Guy entry is where I learned how to use the "Count" Property in the first code sample.
The second code sample is really confusing. In this Script Center reference, the following statement works:
Import-Csv c:\scripts\test.txt | Measure-Object score -ave -max -min
It still works even if it's re-written like so:
Import-Csv c:\scripts\test.txt | Measure-Object -ave -max -min -property score
I don't have too many problems with accepting this until I consider the Measure-Object help page. The parameter definition for
-Property <string[]>
states:The default is the Count (Length) property of the object.
If
Count
is the default, then shouldn't an explicit pass ofCount
work?GPS AcroRd32 | Measure -Property Count # Fails
The following provides me the information I need, except it doesn't provide me with an integer to perform operations on, as you'll 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'm supposed to use Dot Notation, then I will, but I'd like to take this opportunity to learn more about how and *why PowerShell works this way, rather than just building a perfunctory understanding of PowerShell's syntax. To put it another way, I want to avoid turning into a Cargo Cult Programmer/ Code Monkey.
-
Stisfa over 12 years
Get-Member -InputObject (gps AcroRd32)
- this helped me to understand where theCount
Property came from. Now I can see why my "complete statement" doesn't work, since it was the wrong syntax. Thanks for showing me how to use(GPS AcroRd32).Count
!