CodexBloom - Programming Q&A Platform

PowerShell 7.3 Get-Process not filtering by CPU time accurately with -Where clause

👀 Views: 60 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-05
powershell Get-Process filtering

I just started working with I'm trying to filter processes based on their CPU time using PowerShell 7.3, but I'm working with some unexpected behavior. Specifically, I want to retrieve processes that are consuming more than 100 CPU seconds, but when I run my script, it seems to return processes that should not meet this criteria. Here's the code snippet I'm using: ```powershell $threshold = 100 $highCpuProcesses = Get-Process | Where-Object { $_.CPU -gt $threshold } $highCpuProcesses | Select-Object Name, CPU ``` When executing this, I see some processes listed that have a CPU time lower than 100 seconds. I've checked the `$_` object to confirm I'm accessing the CPU property correctly, and it seems valid, but the filter isn't working as expected. I also tried casting the CPU property to a numeric value explicitly, like this: ```powershell $highCpuProcesses = Get-Process | Where-Object { [double]$_.CPU -gt [double]$threshold } ``` Still, I'm seeing incorrect results. I've verified that the percentage CPU usage doesn't seem to correlate with the CPU seconds reported. Is there a known scenario or bug with the `Get-Process` cmdlet in this version, or is there something I'm missing in my approach? Any insights would be appreciated. Am I missing something obvious?