To compare strings in PowerShell, use -eq for equal and -ne for not equal. Both ignore case, so add a c for a case-sensitive comparison, like -ceq:
$city = 'Austin'
"-eq 'AUSTIN': $($city -eq 'AUSTIN')"
"-ceq 'AUSTIN': $($city -ceq 'AUSTIN')"
"-ne 'Denver': $($city -ne 'Denver')"
Output:
-eq 'AUSTIN': True
-ceq 'AUSTIN': False
-ne 'Denver': True

I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. Everything matched except one .NET method overload, which I point out below.
Case-sensitive and case-insensitive comparison
PowerShell operators ignore case, but .NET string methods don’t. That catches people who switch between the two:
$a = 'Austin'
$b = 'AUSTIN'
".Equals(): $($a.Equals($b))"
"[string]::Equals, ignore case: $([string]::Equals($a, $b, [System.StringComparison]::OrdinalIgnoreCase))"
"[string]::Compare, ignore case: $([string]::Compare($a, $b, $true))"
Output:
.Equals(): False
[string]::Equals, ignore case: True
[string]::Compare, ignore case: 0
| To compare | Ignoring case | Matching case |
|---|---|---|
| Equal | -eq | -ceq or .Equals() |
| Wildcard | -like | -clike |
| Regex | -match | -cmatch |
| Contains text | -like '*text*' | .Contains() |
[string]::Compare() returns 0 when the strings are equal, a negative number when the first sorts earlier, and a positive number when it sorts later.
Check if a string contains text: -contains is the wrong tool
-contains checks whether a list has an item. On a single string, it only returns True for an exact match:
$product = 'PowerShell'
"-contains 'Shell': $($product -contains 'Shell')"
"-like '*shell*': $($product -like '*shell*')"
".Contains('Shell'): $($product.Contains('Shell'))"
".Contains('shell'): $($product.Contains('shell'))"
Output:
-contains 'Shell': False
-like '*shell*': True
.Contains('Shell'): True
.Contains('shell'): False

Use -like '*text*' to ignore case, or .Contains() when case matters. PowerShell 7 can also make .Contains() ignore case:
'PowerShell'.Contains('shell', [System.StringComparison]::OrdinalIgnoreCase)
Output in PowerShell 7:
True
That version of .Contains() doesn’t exist in Windows PowerShell 5.1, which stops with a method error, so stick to -like there.
Match patterns with -like and -match
-like uses wildcards like * and ?. -match uses regular expressions:
$file = 'q3-sales-2026.xlsx'
"-like 'q3-*': $($file -like 'q3-*')"
"-like '*.csv': $($file -like '*.csv')"
"-match '\d{4}': $($file -match '\d{4}')"
"-notmatch 'draft': $($file -notmatch 'draft')"
Output:
-like 'q3-*': True
-like '*.csv': False
-match '\d{4}': True
-notmatch 'draft': True
For more pattern examples, see Where-Object with -like, -match and -in.
Alphabetical order, numbers and extra spaces
"'apple' -lt 'Banana': $('apple' -lt 'Banana')"
"'10.0' -eq 10: $('10.0' -eq 10)"
"10 -eq '10.0': $(10 -eq '10.0')"
"'Yes ' -eq 'Yes': $('Yes ' -eq 'Yes')"
"Trimmed first: $('Yes '.Trim() -eq 'Yes')"
Output:
'apple' -lt 'Banana': True
'10.0' -eq 10: False
10 -eq '10.0': True
'Yes ' -eq 'Yes': False
Trimmed first: True

-lt and -gt compare alphabetically and ignore case. When one side is a number, the value on the left decides whether PowerShell compares numbers or text, which is why the two 10.0 lines differ.
A trailing space makes two strings different, and it’s common in data from files and forms. Trim input before comparing. Microsoft explains the conversion rules in about_Comparison_Operators.
Compare strings in if/else
Combine -eq with -in to check one value against several choices in one condition:
foreach ($status in 'Shipped', 'pending', 'Canceled', 'Lost') {
if ($status -eq 'Shipped') {
"$status -> send a tracking email"
}
elseif ($status -in 'Pending', 'Processing') {
"$status -> check again tomorrow"
}
else {
"$status -> call the customer"
}
}
Output:
Shipped -> send a tracking email
pending -> check again tomorrow
Canceled -> call the customer
Lost -> call the customer
Or use switch for many values
switch is easier to read when there are several possible values. It ignores case too, so ca matches ‘CA’:
foreach ($state in 'TX', 'ca', 'NY', 'WA') {
switch ($state) {
'TX' { "$state -> Central time" }
'CA' { "$state -> Pacific time" }
'NY' { "$state -> Eastern time" }
default { "$state -> check the list" }
}
}
Output:
TX -> Central time
ca -> Pacific time
NY -> Eastern time
WA -> check the list
Compare two lists of strings
Compare-Object shows which items are in only one of two lists:
$lastWeek = 'WEB01', 'WEB02', 'SQL01'
$thisWeek = 'WEB01', 'SQL01', 'FILE01'
Compare-Object -ReferenceObject $lastWeek -DifferenceObject $thisWeek | ForEach-Object {
if ($_.SideIndicator -eq '=>') { "Added: $($_.InputObject)" } else { "Removed: $($_.InputObject)" }
}
Output:
Added: FILE01
Removed: WEB02
The => indicator means the item is only in the second list, and <= means it’s only in the first. PowerShell Compare-Object covers it in more detail.
Frequently Asked Questions
How do I compare two strings in PowerShell?
Use $a -eq $b. It returns True or False and ignores case. Use -ceq for a case-sensitive comparison.
Is PowerShell string comparison case-sensitive?
No. -eq, -like and -match ignore case. The c versions (-ceq, -clike, -cmatch) and .NET methods like .Equals() and .Contains() match case.
How do I check if a string contains another string?
Use $text -like '*word*', or $text.Contains('word') for a case-sensitive check. Don’t use -contains, which is for lists.
Why does ‘Yes ‘ -eq ‘Yes’ return False?
The first string has a trailing space. Trim input before comparing: $answer.Trim() -eq 'Yes'.
How do I compare a string against several values?
Use -in, like $status -in 'Pending', 'Processing', or a switch statement for different actions per value.
If you work with strings a lot, these help too:
- Check if a string is null or empty
- Replace text in strings
- Change the case of a string
- PowerShell switch statement
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.