Ezine 178 - Filter Data with PowerShell's -Match and -LikeHow to Filter Data with PowerShell's -Match and -LikeAs with life, scripts often give us too much information. In both settings the answer is a suitable filter. PowerShell provides a rich variety of tools to help us select just the data we need. My favourite method, as we will see, is the 'Where' statement with the '-match' comparitor. Topics for the Comparators -Match and -Like
This Week's SecretIn the case of spreadsheet formulae I admit a fixation with the 'If' statement. Often times it would have been more efficient to use 'Lookup'. With PowerShell scripts, although again I could use the 'If' construction, my preference now is for the 'where' clause. My point is that whether you are designing a spreadsheet or a script, there are numerous ways of achieving the same result. And while some methods are more efficient than others, it's often just a matter of taste which method you choose. This Week's MissionThis week's mission is relatively straightforward; I want to show you PowerShell techniques which filter data. The scenario is that we want research WmiObjects in general and 'system' classes in particular. The problem is we are swamped with names of hundreds of classes. Our solution will be to use the parent construction of a 'Where-Object' clause, with a comparitor of -Match or -Like. -Match and -Like We can distinguish between these two parameter in several ways. In terms of remembering the difference I think of '-Match' as meaning a pure pattern match. Whereas to me, '-Like' is a more fuzzy concept that is going to need the wildcard* symbol. A more technical definition is as follows: -Like is just a wildcard comparison, while -Match is a regular expression, and a superset of -Like. Example 1: The Where Statement with the -Match ComparitorWmiObject is our vehicle for this example. The where-Object statement employs a comparitor to find the pattern "system". Because we use -Match, "system" can be anywhere in the WMI object's name. Incidentally, this combination of 'Where' with '-Match' is my favourite method of filtering data. # PowerShell example to list demonstrate -Match clear-Host Learning PointsNote 1: Take special note of the important construction $_. This means 'in this data stream'. Note 2: See how I have employed PowerShell's signature tune the (|) pipe. The purpose of the pipe is to make the output of the list, the input into the where clause. Challenge 1: (Easy) remove the -Object from the where clause. The script should run just the same. Challenge 2: Substitute the question mark (?) for where-Object. Guy does not like this particular short-cut but many script writers prefer the tighter code which results from using this alias. Example 2: The Where Statement with the -Like ComparitorThe only two changes compared with Example 1 are substituting -Like for -Match and adding a wildcard* in front of system. -Like says to me 'sort of', 'fuzzy', or 'around about'. # PowerShell example to demonstrate the comparitor -Like clear-Host Learning PointsNote 1: Introducing $WMI.count helps us to compare these results with example 1; you should see that -Like produces fewer wmiObjects then -Match. To make -Like show all the "system" object you need to add another wildcard: "*system*". Example 3: The If Statement with the -Match ComparitorTo give you perspective, I have introduced variations, for example employing an 'If' construction instead of the 'Where' in example 1 and 2. clear-Host Learning PointsNote 1: Unlike 'Where', the 'If' statement needs the additional command 'foreach' to kick off the loop through the $_.name. Note 2: The syntax of the 'If' construction is (test) then {script block for matching items} Note 3: In the line: "There are $i objects
containing TCP" Example 4: Introducing -NotMatch also -AndThe most obvious comparitor is 'equals'. You code this in PowerShell with -eq (not =). However in the present examples, to use -eq effectively we would need to know the name of the specific class. Whereas in this context we want broader research into possible names containing 'Win32'. Thus let us look at alternatives to -eq. It is surprising how often the negative -NotMatch produces a neat solution to a scripting problem. For instance there are many WmiObjects beginning with CIM, thus one way of excluding them would be to use -NotMatch "CIM". Just one more point, using multiple criteria for your filter is easy once you master the deceptively simple '-And' syntax. # PowerShell example to list demonstrate -NotMatch and -and clear-PSAhost Learning PointsNote 1: It would make my day if you experimented with different filters. Substitute your ideas for "CIM", and "__". Perhaps best of all would be to combine -NotMatch and -Match. Follow-upAs usual with my scripts, the mission is to get you started. If
you want to know more about -Match, -Like and their relatives, then start
with PowerShell's own help thus: These help files introduce a whole world of specific terms, for example, 'regular expression comparisons' and 'wildcard comparison'. Once you need and understand such extra information, then I have succeeded in my mission of introducing you to -Match and -Like. If you are looking for handy network utilities, try some of the free downloads at Tools4Ever See More Microsoft PowerShell WMI Examples:• Home • PowerShell WMI • WMI Services • Memory • Disk • DNS • Win32_pingstatus • WMI Class • Win32_NetworkAdapter • Win32_NetworkAdapterConfiguration • Disable NIC • Examples • Win32_product • PowerShell -Query • PowerShell -Filter Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
*
| ||||||