Measure-Object
Published: February 29, 2012
Updated: August 15, 2012
Applies To: Windows PowerShell 2.0, Windows PowerShell 3.0
Measure-Object
Aliases
The following abbreviations are aliases for this cmdlet:
- measure
Syntax
Parameter Set: GenericMeasure Measure-Object [[-Property] <String[]> ] [-Average] [-InputObject <PSObject> ] [-Maximum] [-Minimum] [-Sum] [ <CommonParameters>] Parameter Set: TextMeasure Measure-Object [[-Property] <String[]> ] [-Character] [-IgnoreWhiteSpace] [-InputObject <PSObject> ] [-Line] [-Word] [ <CommonParameters>]
Detailed Description
The Measure-Object cmdlet calculates the property values of certain types of object. Measure-Object performs three types of measurements, depending on the parameters in the command.
The Measure-Object cmdlet performs calculations on the property values of objects. It can count objects and calculate the minimum, maximum, sum, and average of the numeric values. For text objects, it can count and calculate the number of lines, words, and characters.
Parameters
-Average
Displays the average value of the specified properties.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Character
Counts the number of characters in the input object.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-IgnoreWhiteSpace
Ignores white space in word counts and character counts. By default, white space is not ignored.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-InputObject<PSObject>
Specifies the objects to be measured. Enter a variable that contains the objects, or type a command or expression that gets the objects.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
none |
|
Accept Pipeline Input? |
true (ByValue) |
|
Accept Wildcard Characters? |
false |
-Line
Counts the number of lines in the input object.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Maximum
Displays the maximum value of the specified properties.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Minimum
Displays the minimum value of the specified properties.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Property<String[]>
Specifies one or more numeric properties to measure. The default is the Count (Length) property of the object.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
1 |
|
Default Value |
Count |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Sum
Displays the sum of the values of the specified properties.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
-Word
Counts the number of words in the input object.
|
Aliases |
none |
|
Required? |
false |
|
Position? |
named |
|
Default Value |
False |
|
Accept Pipeline Input? |
false |
|
Accept Wildcard Characters? |
false |
<CommonParameters>
This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).
Inputs
The input type is the type of the objects that you can pipe to the cmdlet.
-
System.Management.Automation.PSObject
You can pipe objects to Measure-Object.
Outputs
The output type is the type of the objects that the cmdlet emits.
-
Microsoft.PowerShell.Commands.GenericMeasureInfo, Microsoft.PowerShell.Commands.TextMeasureInfo, Microsoft.PowerShell.Commands.GenericObjectMeasureInfo
If you use the Word parameter, Measure-Object returns a TextMeasureInfo object. Otherwise, it returns a GenericMeasureInfo object.
Examples
-------------------------- EXAMPLE 1 --------------------------
This command counts the files and folders in the current directory.
PS C:\> get-childitem | measure-object
-------------------------- EXAMPLE 2 --------------------------
This command displays the minimum, maximum, and sum of the sizes of all files in the current directory, and the average size of a file in the directory.
PS C:\> get-childitem | measure-object -property length -minimum -maximum -average
-------------------------- EXAMPLE 3 --------------------------
This command displays the number of characters, words, and lines in the Text.txt file.
PS C:\> get-content C:\test.txt | measure-object -character -line -word
-------------------------- EXAMPLE 4 --------------------------
This command displays the minimum, maximum, and average sizes of the working sets of the processes on the computer.
PS C:\> get-process | measure-object -property workingset -minimum -maximum -average
-------------------------- EXAMPLE 5 --------------------------
This command calculates the average years of service of the employees of a company.
The ServiceYrs.csv file is a CSV file that contains the employee number and years of service of each employee. The first row in the table is a header row of "EmpNo, Years".
When you use Import-Csv to import the file, the result is a PSCustomObject with note properties of EmpNo and Years. You can use Measure-Object to calculate the values of these properties, just like any other property of an object.
PS C:\> import-csv d:\test\serviceyrs.csv | measure-object -property years -minimum -maximum -average
-------------------------- EXAMPLE 6 --------------------------
This example demonstrates the Measure-Object can measure Boolean values. In this case, it uses the PSIsContainer Boolean property to measure the incidence of folders (vs. files) in the current directory.
PS C:\> get-childitem | measure-object -property psiscontainer -max -sum -min -averageCount : 126Average : 0.0634920634920635Sum : 8Maximum : 1Minimum : 0Property : PSIsContainer
Related topics
Compare-Object
ForEach-Object
Group-Object
New-Object
Select-Object
Sort-Object
Tee-Object
Where-Object