Measure the properties of objects, count the number of items, sum up numeric sizes etc.
Syntax Measure-Object [ [-property] string[] ] [-inputObject psobject] [-average] [-sum] [-minimum] [-maximum] [-StandardDeviation] [-AllStats] [CommonParameters] Measure-Object [ [-property] string[] ] [-inputObject psobject] [-character] [-line] [-word] [-ignoreWhiteSpace] [CommonParameters] Key -property string[] The property to measure. -AllStats Return all the statistics (average,sum,minimum,maximum,StandardDeviation) PowerShell 6+. -average Average the values in the designated property. -character Count the number of characters in the input object. -inputObject The object(s) to be measured. A command, expression or variable that contains the object(s). When the -InputObject parameter is used to submit a collection of items, Measure-Object receives one object that represents the collection. Because one collection object cannot be measured, Measure-Object returns the entire collection unchanged. To convert multiple items, pipe them to Measure-Object. This parameter is an implementation detail: its purpose is to enable input via the pipeline, and its direct use with arrays (collections) does not (yet) provide any useful functionality. -ignoreWhiteSpace Ignore white space in word counts and character counts. -line Count the number of lines in the input object. -minimum Determine the minimum value of the properties. -maximum Determine the maximum value of the properties. -StandardDeviation Determine the standard deviation of the properties. PowerShell 6+. -sum Sum the values of the properties. -word Count the number of words in the input object.
Standard Aliases for Measure-Object: measure
In PowerShell 3.0 it is possible to measure Date/Time properties such as LastWriteTime of a file or StartTime of a process.
By default Measure-Object will return an array of several measurements, if you only want to return one, then pipe the output into
| Select-Object -expand item
e.g. To get a count of Aliases:
Get-Alias | Measure-Object | Select-Object -ExpandProperty Count
Beginning in PowerShell 6, Measure-Object supports measurement of objects by using wildcards in property names.
Count the number of files and folders in the current directory:
PS C:\> get-childitem -force | measure-object
Count the number of files in the current directory:
PS C:\> get-childitem -force | measure-object -property length
Display the size of the largest and the size of the smallest file in the current directory:
PS C:\> get-childitem -force | measure-object -property length -minimum -maximum
Display the total size in MB of the C:\Demo directory with all subfolders/files, -force incudes any hidden files:
PS C:\> (get-childitem 'C:\demo' -force -recurse | measure-object -property Length -sum -ErrorAction SilentlyContinue).sum / 1MB
Display the total size in MB of the C:\Demo directory with all subfolders/files, -force incudes any hidden files:
PS C:\> (get-childitem 'C:\demo' -force -recurse | measure-object -property length -sum -ErrorAction SilentlyContinue | Select-Object -expand sum ) /1MB
Display the total size in GB of the C:\Demo directory with all subfolders/files, rounded to 2 decimal places:
PS C:\> "{0:N2} GB" -f ((get-childitem 'C:\demo' -force -recurse | measure Length -sum -ErrorAction SilentlyContinue).sum / 1Gb)
Count the number of words in the file SS64.txt
PS C:\> get-content C:\SS64.txt | measure-object -word | Select-Object -expand words
Determine the standard deviation for the CPU used by all processes. A large deviation would indicate a small number of processes consuming the most CPU:
Get-Process | Measure-Object -Average -StandardDeviation CPU
Determine the maximum of any type of paged memory usage among a set of processes (wildcards require PowerShell 6+).
Get-Process | Measure-Object -Maximum *paged*memory*size
“The advantage of living is not measured by length, but by use; some men have lived long, and lived little; attend to it while you are in it” ~ Michel Eyquem de Montaigne
Compare-Object - Compare the properties of objects.
Get-ChildItem - Get child items.
ForEach-Object - Loop for each object in the pipeline.
Group-Object - Group the objects that contain the same value for a common property.
New-Object - Create a new .Net object.
Select-Object - Select objects based on parameters set in the Cmdlet command string.
Sort-Object - Sort the input objects by property value.
Tee-Object - Send input objects to two places.
Where-Object - Filter input from the pipeline allowing operation on only certain objects.
Equivalent bash command: expr - Evaluate expressions.