none
Exclude Idle and _Total from Get-Counter Per Process CPU script. RRS feed

  • Question

  • Greetings,

    I am running the script below to collect the TOP 10 Processes consuming CPU.  Is it possible to exclude the processes Idle and _Total?  I tried using "WHERE Name <> '_Total' AND Name <> 'Idle'" but it breaks the script.  Is this possible?  Many thanks as always for any help.

    v/r

    Chris

    _________________

    $CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
    (Get-Counter "\Process(*)\% Processor Time").CounterSamples | Select InstanceName, @{Name="CPU %";Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}} | sort *CPU* -Descending | select -First 10 | Format-Table -AutoSize

    • Moved by Bill_Stewart Wednesday, September 4, 2019 6:35 PM Abandoned
    Thursday, March 14, 2019 12:06 PM

All replies

  • Just add a Where-Object to your pipeline  and exclude those records.


    \_(ツ)_/

    Thursday, March 14, 2019 12:39 PM
  • I tried adding the where clause in several different places but I keep getting "The operator is reserved for future use".  Where do I place there where clause?
    Thursday, March 14, 2019 4:49 PM
  • Didn't you write this code?  It goes in the pipeline after the counter selection.


    \_(ツ)_/

    Thursday, March 14, 2019 4:51 PM
  • Here is the correct way to write this and format the code:

    $select = @(
        'InstanceName',
        @{ n = 'CPU %'; e = {[math]::Round($_.CookedValue/$CpuCores)} }
    )
    $CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
    Get-Counter '\Process(*)\% Processor Time'|
        Select-Object -Expand CounterSamples |
        Where-Object{ $_.InstanceName -notmatch '_total|idle' } |
        Select-Object  $select | 
        Sort-Object 'CPU %' -Descending | 
        Select-Object -First 10
        Format-Table -AutoSize
    


    \_(ツ)_/

    Thursday, March 14, 2019 5:19 PM
  • Didn't you write this code?  It goes in the pipeline after the counter selection.


    \_(ツ)_/

    I pieced it together from other posts.  I am very new to PowerShell and WMI.  We have switched monitoring tools here and what I used to be able to do via a GUI I now have to write scripts for.  I know it may be simple for most but it is a big learning curve for me.

    v/r

    Chris

    Friday, March 15, 2019 10:54 AM
  • Here is the correct way to write this and format the code:

    $select = @(
        'InstanceName',
        @{ n = 'CPU %'; e = {[math]::Round($_.CookedValue/$CpuCores)} }
    )
    $CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
    Get-Counter '\Process(*)\% Processor Time'|
        Select-Object -Expand CounterSamples |
        Where-Object{ $_.InstanceName -notmatch '_total|idle' } |
        Select-Object  $select | 
        Sort-Object 'CPU %' -Descending | 
        Select-Object -First 10
        Format-Table -AutoSize


    \_(ツ)_/

    Thank you very much!

    v/r

    Chris

    Friday, March 15, 2019 10:55 AM