HOWTO · PowerShell

How to Compare the Contents of Two String Objects in PowerShell

This article explores several approaches to comparing the contents of two string objects in PowerShell, from basic equality and inequality checks using operators like -eq and -ne to more advanced techniques involving the method Equals().

On this page

PowerShell offers two different kinds of string checks: use -eq or -ne to compare values, and -like to test a value against a wildcard pattern. The comparison operators ignore letter case by default; add c (-ceq or -cne) when the case must match. A string’s Equals() method is another case-sensitive value comparison.

Choose a PowerShell string comparison method

Choose the operator based on the question you need to answer:

  • Use -eq when you want to know whether two values are equal, or -ne when you want to know whether they differ. Both ignore letter case by default.
  • Use -ceq and -cne for equality or inequality that treats uppercase and lowercase letters as different.
  • Use -like when the right-hand value contains wildcard syntax, such as * for any sequence of characters. It tests a pattern; it does not compare the two strings as literal values.
  • Use Equals() when an instance method call is more convenient and you want a case-sensitive value comparison.

These operators return a Boolean result for two scalar strings. Keep the pattern on the right side of -like so the intent is clear.

Compare strings for equality or inequality with -eq and -ne

Run this example:

$first = "PowerShell"
$second = "PowerShell"

$isEqual = $first -eq $second
$isDifferent = $first -ne $second

Write-Host "Equal: $isEqual"
Write-Host "Unequal: $isDifferent"

For this example, the supplied runtime run printed Equal: True and Unequal: False. With the two identical values shown, -eq represents the equality check and -ne its inverse. In ordinary PowerShell string comparisons, these operators are case-insensitive: for example, "PowerShell" -eq "powershell" is true. Use the case-sensitive operators below if that default is not appropriate.

Make equality and inequality checks case-sensitive with -ceq and -cne

The c prefix makes these comparisons case-sensitive. For example, "PowerShell" -ceq "powershell" is false because the letter casing differs, and "PowerShell" -cne "powershell" is true. Use -ceq to require the same case as well as the same characters; use -cne when a case difference should count as a difference.

The case-sensitive forms are useful for identifiers or tokens whose casing is significant. Choose one comparison rule and apply it consistently where values are validated; otherwise a check may accept variants that another part of a script treats as distinct.

Match wildcard patterns with -like

-like checks whether a target string matches a wildcard pattern. The * wildcard matches any sequence of characters, including no characters, while ? matches one character. For example, "report-2026.csv" -like "report-*.csv" matches, while "report-2026.txt" -like "report-*.csv" does not. These are pattern checks, not literal equality checks.

Like -eq, the -like operator is case-insensitive by default. Use -clike when pattern matching must respect letter case. When the input must equal a literal value, use -eq or -ceq instead; wildcard characters in a -like pattern have special meaning.

Compare string values with Equals()

Call Equals() on the first string and pass the second string as its argument: $first.Equals($second). For strings, this method compares the values case-sensitively, so "PowerShell".Equals("powershell") is false. It does not interpret wildcard characters: "report-2026.csv".Equals("report-*.csv") is false because the asterisk is treated as a literal character in the argument.

For a simple Boolean check, Equals() and -ceq both express case-sensitive equality. Prefer the comparison operators when they make a condition easier to read, and use the method when a method call fits the surrounding code. Use -like only when the task is to match a pattern.