|
Guy recommends : View the effective permissions for a folder or shared drive. Free download try it now! |
Windows PowerShell - Conditional OperatorsIntroduction to PowerShell Conditional OperatorsI think of PowerShell's conditional operators as data filters. Perhaps you are suffering from a common problem - too much information? If so, then choosing the most suitable PowerShell operator: -Match, -Like or -Contains will help you to distil the key facts. Introduction to: -Match -Like and -Contains-Match, -Like and -Contains are all similar PowerShell conditional operators, yet each has a subtle specialization. My advice is keep experimenting until you find the particular conditional operator that suits your circumstance. Have faith that you will be able to manipulate your data with one of this trio, and thus achieve the degree of pattern matching that you seek. Topics for PowerShell's Conditional Operators
Please note: the above operators are in addition to the ubiquitous comparison operators, -eq, 'If' and 'ElseIf'. ♣ Example 1: -Match Conditional OperatorThe 'match' can be anywhere within the string. Moreover, the pattern does not have to be a complete, and this is the biggest benefit of match. -Match can use regular expressions for pattern matching. Incidentally -Match, and the other PowerShell conditional operators, all have a negative form, for example -NotMatch. Example 1a - Match does not have to be at the beginning
$Guy ="Guy Thomas 1949" # Result PS> True Example 1b - Naturally a completely wrong name is no good
$Guy ="Guy Thomas 1949" # Result PS> False Example 1c - Wrong date
$Guy ="Guy Thomas 1949" # Result PS> False Note 1: While Guy Thomas 1949 has '19', it does not have '1939', thus returns False. Example 1d - Wildcard? Rides to the rescue
$Guy ="Guy Thomas 1949" # Result PS> True Note 2: Now that we introduce the wildcard ? in the 3rd position, and given that we get a match for the other 3 digits, the result is now 'True'. Example 1e - WmiObject and Where, using * as a Wildcard Here is a real-life -Match example using WmiObject and a where clause, observe the Wildcard*. Get-WmiObject -List | Where {$_.name -Match "cim*"} Negative -NotMatch For negative conditions, there is an alternative form called -Notmatch.
Guy
Recommends: Free WMI Monitor for PowerShell
|