Simple string match, what am I missing?

I have this in an if statement and today it errors and it never has before. I tried it on another prompt and same error.
What am missing, why is it reading the \h as a escape h? I have tried it with single and double quotes.

PS $SecureKeyPath = 'C:\01servers\hdi'
$SecureKeyPath -match "C:\01servers\hdi"
parsing "C:\01servers\hdi" - Unrecognized escape sequence \h.
At line:2 char:1

  • $SecureKeyPath -match "C:\01servers\hdi"
  •   + CategoryInfo          : OperationStopped: (:) [], ArgumentException
      + FullyQualifiedErrorId : System.ArgumentException

PS $PSVersionTable

Name Value

PSVersion 5.1.18362.145
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.18362.145
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3

You need to escape the \

$SecureKeyPath = 'C:\01servers\hdi\'
$SecureKeyPath -match "C:\\01servers\\hdi\\"



You could also use

$SecureKeyPath -match [Regex]::Escape('C:\01servers\hdi\')

Per about Comparison Operators - PowerShell | Microsoft Docs
“-match Returns true when string matches regex”

So you have to look at the 'C:\01servers\hdi' as a Regular Expression (RegEx)

In RegEx the \ is one of the +*?^$.{}()|/ special characters

To do the desired string comparison you can ‘escape’ the \ by adding another one before it like:

$SecureKeyPath = 'C:\01servers\hdi\'
$SecureKeyPath -match "C:\\01servers\\hdi\\"

Note that -match will return TRUE if $SecureKeyPath includes the pattern “C:\01servers\hdi\”
So the following will also return TRUE

'blablaC:\01servers\hdi\blabla' -match "C:\\01servers\\hdi\\"

If you want to validate that $SecureKeyPath has the value 'C:\01servers\hdi' exactly I would use the -eq operator (not case sensitive) instead of -match

'C:\01servers\hdi\' -eq 'c:\01servers\hdi\'

Yeah, I was thinking I could get away with an exact match if the match had single quotes. But remembering that it is a regex, reminds me that is silly. Thanks!

Yeah, PowerShell knows to interpret a string as literal, but to the regex classes, a string is a string; .NET doesn’t really have the distinction of literal / expandable strings except as syntactic sugar, it’s not a fundamental part of the underlying implementation as much as it is in parts of PowerShell’s code.

As mentioned above, the [regex]::Escape() method is a very handy shortcut to have for when you want a literal match. :slight_smile: