PS_Strings_chart_1.jpg
PS_Strings_chart_11.jpg
PS_Strings_chart_12.jpg
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
String
Array
1
LEGEND
Equality
<value> <op> <value>
Boolean
<array>
<op> <value>
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"
( )
Wildcard
–ieq
"abc" –ceq "Abc"
False
@() –eq "dog"
( )
Regex
"Abc" –ceq "Abc"
True
Equality/negated
<value> <op> <value>
Boolean
<array>
<op> <value>
Sub–list
–ne
"abc" –ne "def"
True
"dog","cat","Dog" –ne "dog"
("cat")
–cne
1
Each operator has three variations:
"abc" –ne "Abc"
False
"dog","cat","Dog" –cne "dog"
("cat","Dog")
> default
(e.g. –eq),
–ine
"abc" –cne "Abc"
True
@() –ne "dog"
( )
> case-sensitive
(e.g. –ceq), and
"Abc" –cne "Abc"
False
> case-insensitive
e.g. –ieq).
Wildcard (glob) 2
<target> <op> <glob>
Boolean
<array>
<op> <glob>
Sub–list
Note that the default in each case is
–like
case–insensitive so –eq
is exactly
"dog" –like "dog*"
True
"f42e","12a8","a000","948f"  –like "[a-f]*"
("f42e","a000")
–clike
equivalent to –ieq; the latter is
"kookaburra" –like "k??k*burra"
True
"f42e","12a8","a000","948f"  –
like "[a-f]"
( )
provided if you have a preference for
–ilike
"kookaburra" –like "k?k*burra"
False
"dove","wren","Warbler" –like "w*"
("wren","Warbler")
being explicit.
"kookaburra" –clike "K*"
False
"dove","wren","Warbler" –clike "w*"
("wren")
See about_Comparison_Operators.
"kookaburra" –clike "[kK]*"
True
2
Wildcards include:
Wildcard/negated
<target> <op> <glob>
Boolean
<array>
<op> <glob>
Sub–list
2
> asterisk (*) for any number of
–notlike
"coelacanth" –notlike "cat"
True
"dove","wren","Warbler" –notlike "w*"
("dove")
chars;
–cnotlike
"dog" –notlike "D?g"
False
"dove","wren","Warbler" –cnotlike "w*"
("dove","Warbler")
> question mark (?) for any single
–inotlike
char;
"dog" –cnotlike "D?g"
True
"dove","wren","Warbler" –notlike "*"
( )
> brackets ([ ]) for single, enumerated  
4
Regular expression
<target> <op> <regex>
Boolean
<array>
<op> <regex>
Sub–list
3
char or char range.
–match
"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.
–imatch
"archaeopteryx" –match "ae|ea"
True
"notebook","book","bookend" –match "book$"
("notebook","book")
3
Regular expressions provide a
"notebook","book","bookend" –match "^book$"
("book")
powerful but complex matching
Regex/negated 3
4
<target> <op> <regex>
Boolean
<array>
<op> <regex>
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
.NET implementation—see Regular
Membership
Not
Available
<target>.contains(<value>)
Boolean
Expression Language Elements
.
contains()
"archaeopteryx".contains("aeo")
True
4
Populates $Matcheswhere:
"archaeopteryx".contains("aeiou")
False
> $Matches[0] contains entire match
Membership
5
<target> <op> <value>
Boolean
<array>
<op> <value>
Boolean
> $Matches
[n] contains nth match
–contains
"dog" –contains "Dog"
True
"dog","dogwood" –contains "Dog"
True
5
–contains
technically only operates
–ccontains
on a list; with a scalar it is equivalent
"dog" –ccontains "Dog"
False
"dog","dogwood" –ccontains "Dog"
False
–icontains
to –eq.
"dog" –contains "d"
False
"dog","dogwood","catfish" –ccontains "cat"
False
6
The switch
statement implicitly uses
Membership/negated
5
<target> <op> <value>
Boolean
<array>
<op> <value>
Boolean
–eq
in selecting a match; specifying
–notcontains
"dog" –notcontains "Dog"
False
"dog","dogwood" –notcontains "Dog"
False
–CaseSensitive
modifies this to –ceq.
–cnotcontains
The –Wildcard
and –Regex
"dog" –cnotcontains "Dog"
True
"dog","dogwood" –cnotcontains "Dog"
True
–inotcontains
parameters may be used to effect
switch ( <value> )
switch ( <array>
)
Switch command
Arbitrary
Arbitrary
(or
6
–like
or –match, respectively.
{
{
# iterates through the list
(or no
no
return
<choice> {<statements>}
<choice> {<statements>}
This syntax applies
Similarly adding –CaseSensitive
<choice> {<statements>}
return
<choice> {<statements>}
value)
to all variants below.
. . .
. . .
modifies these to –clike
or –cmatch.
value)
}
}
Switch syntax even allows specifying
Branch/equality
Switch ("maybe")  {
Switch ("dog","bird","lizard")
{
your own arbitrary operator or more
Null
dog : housepet
Switch
[
–Exact
]
"yes"  
{10}
{ "dog","cat" –contains $_ } { "$_ : housepet"
}
complex Boolean expression: instead
bird : not sure
[
–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")  {
Switch –wildcard –case ("dog","bird","Dog") {
10
dog : not sure
use a code block to specify an
"a??"   {10}
"D*"   
{ "$_ : housepet" }
Switch –Wildcard
bird : housepet
expression, where the standard $_
"b??" {20}
"b??d"  { "$_ : housepet" }
automatic variable references the
[
–CaseSensitive
]
Dog : housepet
default    {$null}
Default { "$_ : not sure"  }
input value.
}
}
See about_Switch.
Branch/regex
Switch –regex ("sR9X2T")
{ 4
switch –regex
("dog", "cat", "catfish", "catbird")
{
20
dog : Null
3
7
This deliberate error shows that
"^[a-l]"
{10}
"cat(?!fish)"  
{ "$_ : land" }
Switch –Regex
cat : land
switch
evaluates every expression
"^[m-y]" {20}
"seal|whale|dolphin|catfish" { "$_ : sea"
}
[
–CaseSensitive
]
catfish : sea
"^[z]"
{99}
"owl|eagle|osprey|catbird"  
{ "$_ : air"
}
unless you use break
statements!
catbird  : land
default
{$null}
default { ("$_
: " + $null)}
8
Select–String
examples use a custom
}
}
catbird  : air
7
ss
alias for brevity.
Select–String
<target>
<op> <value>
string
<target>
<op> <value>
Sub–list
9
This might look like a wildcard, but it
This syntax applies
is a regex! As a wildcard, it would
to all variants below.
have returned ("ab3","abcd") only.
Select–String/equality
"dog" | ss –simple "dog"
"dog","Dog" | ss –simple "dog"
"dog"
("dog","Dog")
ss
–SimpleMatch
Other References:
"dog" | ss –simple "do"
"dog","Dog","dogbone" | ss –case –simple "dog"
"dog"
("dog","dogbone")
8
about_Operators
[
–CaseSensitive ]
Conditional Operators
Not Available
Not
Available
Select–String/wildcard
Operator enumeration
Select–String/regex
"coelacanth" | ss "c..l.*th"
"a1","a2","ab3","AB3" | ss "ab.*"
"coelacanth"
("ab3","AB3")
Mastering PowerShell, chapter 7
ss
[
–CaseSensitive
]
"coelacanth" | ss "c.*"
"a1","a2","ab3","AB3" | ss –case "ab.*"
8
"coelacanth"
("ab3")
Copyright © 2011 Michael Sorens
"ab3","abcd","ado" | ss "ab*" 9
("ab3","abcd","ado")
2011.06.08
 ●Version 1.0.1
Select–String/negated
"dog" | ss –simple -NotMatch "dog"
"dog","Cat","catfish" | ss –not "Cat.*h"
Null
("dog","Cat")
ss
–NotMatch
"dog" | ss –simple -NotMatch "cat"
"dog","Cat","catfish" | ss –simple -not -case "Cat"
"dog"
("dog","catfish")
8
Download the latest version from
[–SimpleMatch ]
"dog" | ss –not ""
"dog","dogbone" | ss –not "dog"
<illegal>
Null
Simple-Talk http://bit.ly/l7g6Fj
[
–CaseSensitive ]