<<

PowerShell String Comparison and List Filtering This reference brings together relevant operators plus key language constructs to compare strings in either scalar or array context. (Available online at Simple-Talk.com at http://bit.ly/l7g6Fj.)

Operator 1 String Array LEGEND Equality Boolean Sub–list –eq "abc" –eq "def" False "dog","dogwood","cat","Dog" –eq "dog" ("dog","Dog") Equality –ceq "abc" –eq "Abc" True "dog","dogwood","cat","Dog" –ceq "Cat" ( ) Wilcard "abc" –ceq "Abc" False @() –eq "dog" ( ) –ieq "Abc" –ceq "Abc" True Regex

Equality/negated Boolean Sub–list ne – "abc" –ne "def" True "dog","cat","Dog" –ne "dog" ("cat") –cne "abc" –ne "Abc" False "dog","cat","Dog" –cne "dog" ("cat","Dog") 1 Each operator has three variations: "abc" –cne "Abc" True @() –ne "dog" ( ) > default (e.g. –eq), –ine "Abc" –cne "Abc" False > case-sensitive (e.g. –ceq), and > case-insensitive e.g. –ieq). Wildcard (glob) 2 Boolean Sub–list Note that the default in each case is like – "dog" –like "dog*" True "f42e","12a8","a000","948f" –like "[a-f]*" ("f42e","a000") case–insensitive so –eq is exactly –clike "kookaburra" –like "k??k*burra" True "f42e","12a8","a000","948f" – like "[a-f]" ( ) equivalent to –ieq; the latter is –ilike "kookaburra" –like "k?k*burra" False "dove","wren","Warbler" –like "w*" ("wren","Warbler") provided if you have a preference for "kookaburra" –clike "K*" False "dove","wren","Warbler" –clike "w*" ("wren") being explicit. See about_Comparison_Operators. "kookaburra" –clike "[kK]*" True

2 Wildcards include: Wildcard/negated 2 Boolean Sub–list –notlike > asterisk (*) for any number of "coelacanth" –notlike "cat" True "dove","wren","Warbler" –notlike "w*" ("dove") chars; cnotlike – "dog" –notlike "?g" False "dove","wren","Warbler" –cnotlike "w*" ("dove","Warbler") > (?) for any single –inotlike "dog" –cnotlike "D?g" True "dove","wren","Warbler" –notlike "*" ( ) char;

> brackets ([ ]) for single, enumerated 3 Boolean 4 Sub–list –match char or char range. "archaeopteryx" –match "arch.*" True "nutria","beaver","muskrat" –match "[mn]u.*" ("nutria","muskrat") Must match input in its entirety. –cmatch "archaeopteryx" –match ".*(ae|ea).*" True "a4.001","b3.902","c3.4he" –match "\.[0-9]{2,}" ("a4.001","b3.902") See about_Wildcards. "archaeopteryx" –match "ae|ea" True "notebook","book","bookend" –match "book$" ("notebook","book") –imatch 3 Regular expressions provide a "notebook","book","bookend" –match "^book$" ("book") powerful but complex matching Regex/negated 3 Boolean 4 Sub–list construct; the PowerShell reference notmatch (about_Regular_Expressions) – "bird" -notmatch "Bird.*" False "dove","wren","Warbler" -notmatch "w.*" ("dove") –cnotmatch documents only a portion of it; "bird" -cnotmatch "Bird.*" True "dove","wren","Warbler" -cnotmatch "w.*" ("dove","Warbler") –inotmatch PowerShell actually supports the full Membership .contains() Boolean Not Available .NET implementation—see Regular Expression Language Elements . contains() "archaeopteryx".contains("aeo") True "archaeopteryx".contains("aeiou") False 4 Populates $Matches where: > $Matches[0] contains entire match Membership Boolean 5 Boolean > $Matches [n] contains nth match –contains "dog" –contains "Dog" True "dog","dogwood" –contains "Dog" True 5 –contains technically only operates –ccontains "dog" –ccontains "Dog" False "dog","dogwood" –ccontains "Dog" False on a list; with a scalar it is equivalent –icontains "dog" –contains "d" False "dog","dogwood","catfish" –ccontains "cat" False to –eq.

Membership/negated Boolean 5 Boolean 6 The switch statement implicitly uses notcontains –eq in selecting a match; specifying – "dog" notcontains "Dog" False "dog","dogwood" notcontains "Dog" False –cnotcontains – – –CaseSensitive modifies this to –ceq. "dog" –cnotcontains "Dog" True "dog","dogwood" –cnotcontains "Dog" True The –Wildcard and –Regex –inotcontains parameters may be used to effect Switch command 6 switch ( ) Arbitrary switch ( ) Arbitrary (or { { # iterates through the list –like or –match, respectively. This syntax applies {} (or no {} no return {} return {} value) Similarly adding –CaseSensitive to all variants below. . . . value) . . . modifies these to –clike or –cmatch. } }

Switch syntax even allows specifying Branch/equality Switch ("maybe") { Null Switch ("dog","bird","lizard") { dog : housepet your own arbitrary operator or more Switch [ –Exact ] "yes" {10} { "dog","cat" –contains $_ } { "$_ : housepet" } bird : not sure complex Boolean expression: instead [ –CaseSensitive ] "no" {20} Default { "$_ : not sure" } of specifying a choice as a simple } } lizard : not sure value (string, number, or variable) Branch/wildcard 2 Switch –wildcard ("a13") { 10 Switch –wildcard –case ("dog","bird","Dog") { dog : not sure use a code block to specify an Switch –Wildcard "a??" {10} "D*" { "$_ : housepet" } bird : housepet expression, where the standard $_ "??" {20} "b??d" { "$_ : housepet" } automatic variable references the [ –CaseSensitive ] default {$null} Default { "$_ : not sure" } Dog : housepet } } input value. See about_Switch. Branch/regex 3 Switch –regex ("sR9X2T") { 4 20 switch –regex ("dog", "cat", "catfish", "catbird") { dog : Null Switch Regex "^[a-l]" {10} "cat(?!)" { "$_ : land" } cat : land 7 This deliberate error shows that – "^[m-y]" {20} "seal|whale|dolphin|catfish" { "$_ : sea" } catfish : sea switch evaluates every expression [ –CaseSensitive ] "^[z]" {99} "owl|eagle|osprey|catbird" { "$_ : air" } unless you use break statements! default {$null} default { ("$_ : " + $null) } catbird : land } } catbird : air 7 8 Select–String examples use a custom

ss alias for brevity. Select–String string Sub list – This syntax applies 9 This might look like a wildcard, but it to all variants below. is a regex! As a wildcard, it would Select–String/equality "dog" | ss –simple "dog" "dog" "dog","Dog" | ss –simple "dog" ("dog","Dog") have returned ("ab3","abcd") only. ss 8 –SimpleMatch "dog" | ss –simple "do" "dog" "dog","Dog","dogbone" | ss –case –simple "dog" ("dog","dogbone") Other References: [ –CaseSensitive ] about_Operators Not Available Not Available Conditional Operators Select–String/wildcard Operator enumeration Select–String/regex "coelacanth" | ss "c..l.*th" "coelacanth" "a1","a2","ab3","AB3" | ss "ab.*" ("ab3","AB3") Mastering PowerShell, chapter 7 ss 8 [ –CaseSensitive ] "coelacanth" | ss "c.*" "coelacanth" "a1","a2","ab3","AB3" | ss –case "ab.*" ("ab3")

"ab3","abcd","ado" | ss "ab*" 9 ("ab3","abcd","ado") Copyright © 2011 Michael Sorens

"dog" | ss simple -NotMatch "dog" "dog","Cat","catfish" | ss not "Cat.*h" 2011.06.08 ● Version 1.0.1 Select–String/negated – Null – ("dog","Cat") ss 8 –NotMatch "dog" | ss –simple -NotMatch "cat" "dog" "dog","Cat","catfish" | ss –simple -not -case "Cat" ("dog","catfish") Download the latest version from [–SimpleMatch ] "dog" | ss –not "" "dog","dogbone" | ss –not "dog" Null Simple-Talk http://bit.ly/l7g6Fj

[ –CaseSensitive ]