powershellescapingbackticks

String -like comparison failing when it includes "`" (Acute) character in PowerShell


Below comparison is giving false result in PowerShell , Want it to be true. ` operator is causing it to be false whereas for any other special character it is returning true.

> 'abc`@01' -like 'abc`@01'
False

Solution

  • -like is a wildcard comparison operator and ` is a wildcard escape sequence.

    PS ~> 'abc`@01' -like 'abc``@01'
    True
    

    Use -eq if you want an exact string comparison without having to worry about escaping the reference string:

    PS ~> 'abc`@01' -eq 'abc`@01'
    True