How to use Measure-Object in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

Measure-Object in PowerShell is used to measure the property of the command. There are various measurement parameters are available. For example, Average, Count, sum, maximum, minimum and more.

Example

Get-Process | Measure-Object

Output

PS C:\WINDOWS\system32> Get-Process | Measure-Object
Count       : 278
Average     :
Sum         :
Maximum     :
Minimum     :
Property    :

Here, in the above output, there are total 278 processes are running. If you want to check the maximum memory usage then you can use WorkingSet property with − Maximum Parameter.

Get-Process | Measure-Object -Property WorkingSet -Maximum

Output

PS C:\WINDOWS\system32> Get-Process | Measure-Object -Property WorkingSet –
Maximum
Count       : 277
Average     :
Sum         :
Maximum     : 353447936
Minimum     :
Property    : WorkingSet

You can also use the multiple parameters together like Maximum, Minimum, Sum (To get the total memory consumed in this example) and Average (To get average of memory usage in this example).

Get-Process | Measure-Object -Property WorkingSet -Maximum -Minimum -Sum -
Average

output

Count       : 275
Average     : 37769618.1527273
Sum         : 10386644992
Maximum     : 347447296
Minimum     : 8192
Property    : WorkingSet

For the text file or the string, you can use measurement properties like Line, Word, character, etc.

Example

Get-Content D:\Temp\testreadC.txt | Measure-Object

Output

Count          : 232
Average        :
Sum            :
Maximum        :
Minimum        :
Property       :

To get the Line, Word and Character count,

Get-Content D:\Temp\testreadC.txt | Measure-Object -Line -Word -Character

Output

Lines       Words       Characters    Property
-----       -----       ----------     --------
  229        1829            27156

You can also ignore the whitespace and count the characters.

Get-Content D:\Temp\testreadC.txt | Measure-Object -Line -Word -Character -IgnoreWhiteSpace

Output

Lines       Words       Characters       Property
-----       -----       ----------       --------
  229        1829             7424
raja
Published on 07-Apr-2020 15:42:05
Advertisements