Bird
0
0

Given a list of processes with properties Name and CPU, how do you sort by CPU descending but treat missing CPU values as zero?

hard📝 Application Q9 of 15
PowerShell - Cmdlets and Pipeline
Given a list of processes with properties Name and CPU, how do you sort by CPU descending but treat missing CPU values as zero?
A$processes | Sort-Object -Property CPU -Descending | Where-Object { $_.CPU -ne $null }
B$processes | Sort-Object -Property CPU -Descending -ErrorAction SilentlyContinue
C$processes | Sort-Object -Property @{Expression={ $_.CPU -or 0 }} -Descending
D$processes | Sort-Object -Property CPU -Descending | ForEach-Object { if ($_.CPU -eq $null) { $_.CPU = 0 } }
Step-by-Step Solution
Solution:
  1. Step 1: Handle missing CPU values in sorting

    Use a calculated property with Expression to replace null with 0.
  2. Step 2: Analyze options

    $processes | Sort-Object -Property @{Expression={ $_.CPU -or 0 }} -Descending uses a script block to return CPU or 0, correctly sorting with missing values treated as zero.
  3. Final Answer:

    $processes | Sort-Object -Property @{Expression={ $_.CPU -or 0 }} -Descending -> Option C
  4. Quick Check:

    Use calculated property to handle nulls in Sort-Object [OK]
Quick Trick: Use script block in -Property to handle nulls [OK]
Common Mistakes:
  • Ignoring null values causing errors
  • Filtering out nulls instead of replacing
  • Trying to assign values inside pipeline incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes