external help file | Locale | Module Name | ms.date | online version | schema | title |
---|---|---|---|---|---|---|
Microsoft.PowerShell.Commands.Utility.dll-Help.xml |
en-US |
Microsoft.PowerShell.Utility |
12/12/2022 |
2.0.0 |
Measure-Object |
Calculates the numeric properties of objects, and the characters, words, and lines in string objects, such as files of text.
Measure-Object [-InputObject <PSObject>] [[-Property] <String[]>] [-Sum] [-Average] [-Maximum] [-Minimum]
[<CommonParameters>]
Measure-Object [-InputObject <PSObject>] [[-Property] <String[]>] [-Line] [-Word] [-Character]
[-IgnoreWhiteSpace] [<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. You can use
Measure-Object
to count objects or count objects with a specified Property. You can also use
Measure-Object
to calculate the Minimum, Maximum, Sum, StandardDeviation and
Average of numeric values. For String objects, you can also use Measure-Object
to
count the number of lines, words, and characters.
This command counts the files and folders in the current directory.
Get-ChildItem | Measure-Object
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.
Get-ChildItem | Measure-Object -Property length -Minimum -Maximum -Sum -Average
This command displays the number of characters, words, and lines in the Text.txt file.
Without the Raw parameter, Get-Content
outputs the file as an array of lines.
The first command uses Set-Content
to add some default text to a file.
"One", "Two", "Three", "Four" | Set-Content -Path C:\Temp\tmp.txt
Get-Content C:\Temp\tmp.txt | Measure-Object -Character -Line -Word
Lines Words Characters Property
----- ----- ---------- --------
4 4 15
This example counts the number of objects that have a DisplayName property. The first two
commands retrieve all the services and processes on the local machine. The third command counts the
combined number of services and processes. The last command combines the two collections and pipes
the result to Measure-Object
.
The System.Diagnostics.Process object does not have a DisplayName property, and is left out of the final count.
$services = Get-Service
$processes = Get-Process
$services + $processes | Measure-Object
$services + $processes | Measure-Object -Property DisplayName
Count : 682
Average :
Sum :
Maximum :
Minimum :
Property :
Count : 290
Average :
Sum :
Maximum :
Minimum :
Property : DisplayName
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.
Import-Csv d:\test\serviceyrs.csv | Measure-Object -Property years -Minimum -Maximum -Average
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.
Get-ChildItem | Measure-Object -Property psiscontainer -Maximum -Sum -Minimum -Average
Count : 126
Average : 0.0634920634920635
Sum : 8
Maximum : 1
Minimum : 0
StandardDeviation :
Property : PSIsContainer
The following example measures the number of lines, first a single string, then across several
strings. The newline character `n
separates strings into multiple lines.
# The newline character `n separates the string into separate lines, as shown in the output.
"One`nTwo`nThree"
"One`nTwo`nThree" | Measure-Object -Line
One
Two
Three
Lines Words Characters Property
----- ----- ---------- --------
3
# The first string counts as a single line.
# The second string is separated into two lines by the newline character.
"One", "Two`nThree" | Measure-Object -Line
Lines Words Characters Property
----- ----- ---------- --------
3
# The Word switch counts the number of words in each InputObject
# Each InputObject is treated as a single line.
"One, Two", "Three", "Four Five" | Measure-Object -Word -Line
Lines Words Characters Property
----- ----- ---------- --------
3 5
Indicates that the cmdlet displays the average value of the specified properties.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: GenericMeasure
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Indicates that the cmdlet counts the number of characters in the input objects.
Note
The Word, Char and Line switches count inside each input object, as well as across input objects. See Example 7.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: TextMeasure
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Indicates that the cmdlet ignores white space in character counts. By default, white space is not ignored.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: TextMeasure
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
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 is treated as a single object.
It is recommended that you use Measure-Object
in the pipeline if you want to measure a
collection of objects based on whether the objects have specific values in defined properties.
Type: System.Management.Automation.PSObject
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
Indicates that the cmdlet counts the number of lines in the input objects.
Note
The Word, Char and Line switches count inside each input object, as well as across input objects. See Example 7.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: TextMeasure
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Indicates that the cmdlet displays the maximum value of the specified properties.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: GenericMeasure
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Indicates that the cmdlet displays the minimum value of the specified properties.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: GenericMeasure
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Specifies one or more properties to measure. If you do not specify any other measures,
Measure-Object
counts the objects that have the properties you specify.
Type: System.String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: True
Indicates that the cmdlet displays the sum of the values of the specified properties.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: GenericMeasure
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Indicates that the cmdlet counts the number of words in the input objects.
Note
The Word, Char and Line switches count inside each input object, as well as across input objects. See Example 7.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: TextMeasure
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
You can pipe objects to this cmdlet.
By default, this cmdlet returns a GenericMeasureInfo object.
When you use the Word parameter, this cmdlet returns a TextMeasureInfo object.
Windows PowerShell includes the following aliases for Measure-Object
:
measure