Definition: Returns a zero-based array containing a subset of a string array based on specified filter criteria.


To tell you the truth, we’ve never actually seen anyone use the Filter function; however, we can see where it might be useful. Given an array, Filter enables you to do a wildcard search of items within that array. For example, suppose you have an array with the following items:


·         Monday

·         Month

·         Merry

·         Mansion

·         Modest


Using the Filter function and the wildcard value mon* would return a sub-array containing all the values that start with the letters m-o-n:


·         Monday

·         Month


In Windows PowerShell you create a similar substring by using the Where-Object Cmdlet and weeding out only those values that start with mon (note that you use the –like operator to do a wildcard search like that). The first of the following two commands creates an array named $a and assigns five string values to that array. The second command takes $a and pipes the values to Where-Object, which selects all the values that start with mon and assigns them to the variable $b:


$a ="Monday","Month","Merry","Mansion","Modest"
$b = ($a | where-object {$_ -like "Mon*"})


When you run this command and then echo back the value of $b you should get the following: