|
| 1 | +$tip = [tiPS.PowerShellTip]::new() |
| 2 | +$tip.CreatedDate = [DateTime]::Parse('2025-04-29') |
| 3 | +$tip.Title = 'Use Measure-Object to get stats about objects' |
| 4 | +$tip.TipText = @' |
| 5 | +You can use the Measure-Object cmdlet to get statistics about objects in PowerShell. This cmdlet can be used to calculate the sum, average, minimum, maximum, and count of numeric properties in objects. When used with text input, it can count characters, words, and lines. |
| 6 | +
|
| 7 | +The cmdlet returns an object containing properties for each statistic, but the statistic is only actually calculated if you provided the switch for it. For example, if you only provide the -Sum switch, the Sum property will contain the sum of the values, but the Average property will be null since the -Average switch was not provided. |
| 8 | +'@ |
| 9 | +$tip.Example = @' |
| 10 | +# Get all statistics about a range of numbers. |
| 11 | +1..10 | Measure-Object -Average -Sum -Minimum -Maximum -StandardDeviation |
| 12 | +
|
| 13 | +# Get all statistics about a string. |
| 14 | +"Hello there" | Measure-Object -Character -Word -Line |
| 15 | +
|
| 16 | +# Count the number of words in a file. |
| 17 | +Get-Content 'C:\path\to\file.txt' | Measure-Object -Word |
| 18 | +
|
| 19 | +# Calculate the total size (Length) of all files in the current directory. |
| 20 | +Get-ChildItem | Measure-Object -Property Length -Sum |
| 21 | +
|
| 22 | +# In an array of objects, find the one with the maximum value of the Num property. |
| 23 | +@{num=3}, @{num=4}, @{num=5} | Measure-Object -Maximum Num |
| 24 | +
|
| 25 | +# Get the total and maximum CPU time and paged memory size of all processes. |
| 26 | +Get-Process | Measure-Object -Property CPU,PagedMemorySize -Sum -Maximum |
| 27 | +'@ |
| 28 | +$tip.Urls = @( |
| 29 | + 'https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object' |
| 30 | + 'https://adamtheautomator.com/powershell-measure-object/' |
| 31 | +) |
| 32 | +$tip.Category = [tiPS.TipCategory]::NativeCmdlet # Community, Editor, Module, NativeCmdlet, Performance, Security, Syntax, Terminal, or Other. |
| 33 | +$tip.Author = 'Daniel Schroeder (deadlydog)' # Optional. Get credit for your tip. e.g. 'Daniel Schroeder (deadlydog)'. |
| 34 | +#$tip.ExpiryDate = [DateTime]::Parse('2024-10-30') # Optional. If the tip is not relevant after a certain date, set the expiration date. e.g. Announcing a conference or event. |
| 35 | + |
| 36 | +# Category meanings: |
| 37 | +# Community: Social events and community resources. e.g. PowerShell Summit, podcasts, etc. |
| 38 | +# Editor: Editor tips and extensions. e.g. VS Code, ISE, etc. |
| 39 | +# Module: Modules and module tips. e.g. PSScriptAnalyzer, Pester, etc. |
| 40 | +# NativeCmdlet: Native cmdlet tips. e.g. Get-Process, Get-ChildItem, Get-Content, etc. |
| 41 | +# Performance: Tips to improve runtime performance. e.g. foreach vs ForEach-Object, ForEach-Object -Parallel, etc. |
| 42 | +# Security: Security tips. e.g. ExecutionPolicy, Constrained Language Mode, passwords, etc. |
| 43 | +# Syntax: Syntax tips. e.g. splatting, pipeline, keywords, etc. |
| 44 | +# Terminal: Terminal shortcuts and tips. e.g. PSReadLine, Windows Terminal, ConEmu, etc. |
| 45 | +# Other: Tips that don't fit into any of the other categories. |
0 commit comments