Creating Pipeline Filters

by Oct 18, 2011

In a previous tip, we illustrated how a function can run inside a PowerShell pipeline. That's an excellent way to create filters. Here is a filter that will only display processes that have a "MainWindowTitle" text, thus filtering all running processes and showing only those that have an application window. It then displays the three desktop applications with the most CPU usage:

function Filter-ApplicationProgram {
  param(
    [Parameter(ValueFromPipeline=$true)]
    $Incoming
  )
  
  process {
    if ($incoming.MainWindowTitle -ne '') {
      $incoming
    }
  }
}

Get-Process | Filter-ApplicationProgram | 
  Sort-Object CPU -Descending | 
  Select-Object CPU, Name, MainWindowTitle -First 3

Twitter This Tip!
ReTweet this Tip!