Bird
0
0

How can you calculate both the sum and count of a numeric property Value in a list of objects using Measure-Object in one command?

hard📝 Application Q9 of 15
PowerShell - Working with Objects
How can you calculate both the sum and count of a numeric property Value in a list of objects using Measure-Object in one command?
A$list | Measure-Object -Property Value -Sum | ForEach-Object { $_.Sum; $_.Count }
B$list | Measure-Object -Property Value -Sum -Count
C$list | Measure-Object -Property Value -Sum | Select-Object Sum, Average
D$list | Measure-Object -Property Value -Sum; $list | Measure-Object -Property Value -Count
Step-by-Step Solution
Solution:
  1. Step 1: Understand Measure-Object limitations

    Measure-Object does not accept multiple statistic parameters like -Sum and -Count together.
  2. Step 2: Use pipeline and ForEach-Object to extract both properties

    Measure-Object returns an object with Sum and Count properties; use ForEach-Object to access both.
  3. Final Answer:

    $list | Measure-Object -Property Value -Sum | ForEach-Object { $_.Sum; $_.Count } -> Option A
  4. Quick Check:

    Extract multiple stats from result object using ForEach-Object [OK]
Quick Trick: Extract multiple stats from Measure-Object output object [OK]
Common Mistakes:
  • Trying multiple stats in one call
  • Running Measure-Object twice unnecessarily
  • Incorrect Select-Object usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes