Monthly Archive

Categories

Working with Strings: Simple comparisons

Lets start with simple string matching as its a task that frequently comes along

"aaa" -eq "aaa"

"aaa" -ceq "aaa"

 

both return true.  What’s –ceq you ask?

PowerShell by design is case insensitive so for most operations it treats “aaa” and “AAA” identically.

using –eq tests in  a case insensitive manner and –ceq tests case sensitively so

"aaa" -ceq "AAA"    returns false but

"aaa" -eq "AAA"  returns true

 

Just to add to the complication we also have –ieq which tests in a case insensitive manner so

"aaa" -ieq "AAA" returns true as does

"aaa" -ieq "aaa"

 

Most of the time –eq will meet all your needs with –ceq as backup for case sensitive tests.  I rarely use –ieq

 

In other languages where the comparisons were case sensitive we would need to do one of these

"aaa".ToUpper() -ceq "AAA"

"aaa" -ceq "AAA".ToLower()

 

to avoid case issues but PowerShell just works as

"aaa" -eq "AAA"

 

When comparing strings use –eq if case doesn’t matter and –ceq if it does

Leave a Reply