24/7/365 Support

Program: Simplify Most Where-Object Filters in Windows PowerShell

The WhereObject cmdlet is incredibly powerful, in that it allows you to filter your output based on arbitrary criteria. For extremely simple filters (such as filtering based only on a comparison to a single property), though, the syntax can get a little ungainly:

GetProcess | WhereObject { $_.Handles gt 1000 }

load all the syntax to the script itself:

GetProcess | CompareProperty Handles gt 1000 GetChildItem | CompareProperty PsIsContainer

With a shorter alias, this becomes even easier to type:

PS >gt;SetAlias wheres CompareProperty PS >GetChildItem | wheres Length gt 100

Example 23 implements this “simple where” functionality. Note that supplying a nonexisting operator as the $operator parameter will generate an error message.

Example 23. CompareProperty.ps1

############################################################################## ## CompareProperty.ps1 ## ## Compare the property you provide against the input supplied to the script. ## This provides the functionality of simple WhereObject comparisons without ## the syntax required for that cmdlet. ## ## Example: ## GetProcess | CompareProperty Handles gt 1000 ## dir | CompareProperty PsIsContainer ############################################################################## param($property, $operator = "eq", $matchText = "$true")

Begin { $expression = "`$_.$property $operator `"$matchText`"" } Process { if(InvokeExpression $expression) { $_ } }

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say