powershellbackslash

Searching for backslash in a string with Powershell


I need to search for the amount of backslashes in a string to determine some file path parameters. I have not worked out a way to seach for a backslash without Powershell thinking it is an escapee character.

([regex]::Matches($FilePath, "\" )).count 

Or

$a -match "\"

These both come up with an error "Illegal \ at end of pattern"

Thanks!


Solution

  • You can escape a backslash with a backslash:

    [Regex]::Matches($FilePath, "\\").Count