Powershell String Comparisons : Case-Sensitive and Case-Insensitive (Ignore Case)

Always there is more amount of chance to work with a string value in Powershell. I am also worked with string values and used lot of string compare checks in my scripts. We all know this is an easy job, but sometimes we need to think the comparison check is actually considering the case-sensitive or ignore case. So in the post, I am going to list the set of samples for string comparison.

You can use the below examples both in IF statement and Where-Object.

$strVal ='Hello world'
#If check statement 
if($strVal -eq 'hello world') { //do something }
#where object check statement
Get-Process | Where-Object { $_.Name -eq 'svchost' }

Equal Check – Ignore Case

The normal powershell -eq operator is designed to perform case insensitive comparison and it will ignore the case while comparing the string values.

"Hello World" -eq "hello world" # return True

"Hello World" -eq "Hello World" # return True

Even though the -eq operator performs string comparison in case-insensitive way, you may still want to ensure a case insensitive comparison for some cases, in that place you can use the operator -ieq. The usage of this operator is very less because most people use -eq which does the same job.

"Hello World" -ieq "hello world" # return True

Equal Check – Case-Sensitive

As the normal powershell -eq operator is designed to perform case insensitive comparison, you may need to enforce case-sensitive string compare in some cases, for this case you can use the operator -ceq which compare two string values with case sensitive check.

"Hello World" -ceq "hello world" # return False

"Hello World" -ceq "Hello World" # return True

Contains Check – Ignore Case

We can use the -like operator for contains check with case insensitive operation.

"Hello World" -like "*world*" # return True

"Hello World" -like "*World*" # return True
Note: You can not use the comparison operator contains to check the contains string, because it's designed to tell you if a
powershell array object includes ('contains') a particular object

Contains Check – Case-Sensitive

To perform a case sensitive comparison just prefix the word “c” with like operator (“clike”).

"Hello World" -clike "*world*" # return False

"Hello World" -clike "*World*" # return True

Startswith – Ignore Case

We can use the same operator “like” for starts with comparison by just putting the wildcard character (*) at the end.

"Hello World" -like "hello*" # return True

"Hello World" -like "Hello*" # return True

Startswith – Case-Sensitive

"Hello World" -clike "hello*" # return False

"Hello World" -clike "Hello*" # return True

Endswith – Ignore Case

For ends with check, we need to put the wildcard character (*) at the start of the string.

"Hello World" -like "*world" # return True

"Hello World" -like "*World" # return True

Endswith – Case-Sensitive

"Hello World" -clike "*world" # return False

"Hello World" -clike "*World" # return True

Check Array Contains String – Ignore Case

We can use the operator contains to check whether a powershell string array includes a string word or not.

@("abc", "def") -contains "ABC" # return True

@("abc", "def") -contains "abc" # return True

Check Array Contains String – Case-Sensitive

@("abc", "def") -ccontains "ABC" # return False

@("abc", "def") -ccontains "abc" # return True

Advertisement

Leave a Comment