Measure-Object
Measure-Object [[-Property] <String[]>] [-Average] [-InputObject <PSObject>] [-Maximum] [-Minimum] [-Sum][<CommonParameters>]
Measure-Object [[-Property] <String[]>] [-Character] [-IgnoreWhiteSpace] [-InputObject <PSObject>] [-Line] [-Word][<CommonParameters>]
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 [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates that the cmdlet displays the average value of the specified properties.
-Character [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates that the cmdlet counts the number of characters in the input object.
-IgnoreWhiteSpace [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates that the cmdlet ignores white space in word counts and character counts. By default, white space is not ignored.
-InputObject <PSObject>
- Default value is None
- Accepts pipeline input ByValue
Specifies the objects to be measured. Enter a variable that contains the objects, or type a command or expression that gets the objects.
When you use the InputObject parameter with Measure-Object , instead of piping command results to Measure-Object , the InputObject value-even if the value is a collection that is the result of a command, such as `-InputObject (Get-Process)`-is treated as a single object. Because InputObject cannot return individual properties from an array or collection of objects, it is recommended that if you use Measure-Object to measure a collection of objects for those objects that have specific values in defined properties, you use Measure-Object in the pipeline, as shown in the examples in this topic.
-Line [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates that the cmdlet counts the number of lines in the input object.
-Maximum [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates that the cmdlet displays the maximum value of the specified properties.
-Minimum [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates that the cmdlet displays the minimum value of the specified properties.
-Property <String[]>
- Default value is None
- Accepts pipeline input False
Specifies one or more numeric properties to measure. The default is the Count property of the object.
-Sum [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates that the cmdlet displays the sum of the values of the specified properties.
-Word [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates that the cmdlet counts the number of words in the input object.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.
Inputs
System.Management.Automation.PSObjectOutputs
Microsoft.PowerShell.Commands.GenericMeasureInfo, Microsoft.PowerShell.Commands.TextMeasureInfo,Examples
-
Count the files and folders in a directory:
PS C:\> Get-ChildItem | Measure-Object
This command counts the files and folders in the current directory.
-
Measure the files in a directory:
PS C:\> Get-ChildItem | Measure-Object -Property length -Minimum -Maximum -Average
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.
-
Measure text in a text file:
PS C:\> Get-Content C:\test.txt | Measure-Object -Character -Line -Word
This command displays the number of characters, words, and lines in the Text.txt file.
-
Measure computer processes:
PS C:\> Get-Process | Measure-Object -Property workingset -Minimum -Maximum -Average
This command displays the minimum, maximum, and average sizes of the working sets of the processes on the computer.
-
Measure the contents of a CSV file:
PS C:\> Import-Csv d:\test\serviceyrs.csv | Measure-Object -Property years -Minimum -Maximum -Average
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.
-
Measure Boolean values:
PS C:\> Get-ChildItem | Measure-Object -Property psiscontainer -Max -Sum -Min -Average Count : 126 Average : 0.0634920634920635 Sum : 8 Maximum : 1 Minimum : 0 Property : PSIsContainer
This example demonstrates how 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.
Additional Notes
This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.
PowerShell Commands