Windows PowerShell - Conditional OperatorsIntroduction to PowerShell Conditional OperatorsI think of PowerShell's conditional operators as filters. Perhaps you are suffering from a common problem - too much information? If so, then these PowerShell operators: -match, -like and -contains will help you to distil the key information. Introduction to: -match -like -contains.-Match -like and -contains are all similar PowerShell conditional operators, yet each has a subtle specialization. My best advice is keep experimenting until you find the particular conditional operator that suits your circumstance. Have faith that you will be able to manipulate one of this trio, and thus achieve the degree of filtering that you seek. Also note, these operators are in addition to the ubiquitous, -eq, 'If' and 'elseif' constructions. Topics for PowerShell's Conditional OperatorsExample 1 -MatchThe 'match' can be anywhere in the string. Moreover, the pattern does not have to be a complete match, neither does it have to be at the beginning of the string. -Match uses regular expressions for pattern matching. Incidentally -match and the other PowerShell conditional operators all have a negative form, for example -notmatch. Example 1a - Does not have to be at the beginning
$Guy ="Guy Thomas 1949" Example 1b - Completely wrong name
$Guy ="Guy Thomas 1949" Example 1c - Wrong date
$Guy ="Guy Thomas 1949" Example 1d - Wildcard ? Rides to the rescue
$Guy ="Guy Thomas 1949" Example 1e - Wmiobject and Where get-wmiobject -list | where {$_.name -match "cim*"} Negative -notmatch For negative conditions, there is an alternative form called -notmatch. Example 2 -LikeWith -like, both sides of the expression have to be the same, fortunately, you can employ the usual wildcards * and ?. Example 2a - Having only part of the string is no good
$Guy ="Guy Thomas 1949" Example 2b - Just the start of the string not enough
$Guy ="Guy Thomas 1949" Example 2c - Wildcard * is useful
$Guy ="Guy Thomas 1949" Example 2d - Wildcard * but the rest has to be correct
$Guy ="Guy Thomas 1949" Example 2e - * Wildcard * Double wildcards are handy
$Guy ="Guy Thomas 1949" If your logic needs the negative, then try -notlike. In addition, -notlike and -like have variants that force case sensitivity. -clike and -cnotlike. Difference Between -like and -match
$Guy ="Guy Thomas 1949"
$Guy ="Guy Thomas 1949" Example 3 -ContainsThe conditional operator -contains is similar to -eq, except it returns 'True' or 'False'. -contains is designed for situations where you have a collection and wish to test one particular item.
Example 3a - Checks each item between the commas
Example 3b - Needs exact equality
Example 3c - Wildcards do no good
˚
Summary of PowerShell Conditional OperatorsSo often we suffer from information overload. Working with PowerShell is no different, however it does supply three conditional operators to filter your information: -match, -like and –contains. Each operator has different properties; with research, you can get just the filter you need, and thus filter the desired stream of information into your script’s output. See Also PowerShell Tutorials• PowerShell Home • If Statement • Conditional Operators • Switch • Loops • Brackets 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.
*
|
||||||