Select-string escape character Select-string escape character powershell powershell

Select-string escape character


Try this using single quotes around your search string and specifying SimpleMatch.

Get-ChildItem -path c:\bats\ -recurse | Select-string -pattern '\\server\public' -SimpleMatch


To expand more on the problem since the solution is in @campbell.rw's answer. The Select-String parameter -Pattern supports regular expressions. The backslash is a control character and needs to be escaped. It is not that you need to escape it from PowerShell but the regex engine itself. The escape character is also a backslash

Select-string -pattern '\\\\server\\public'

You can use a static method from the regex class to do that hard work for you.

Select-string -pattern ([regex]::Escape('\\server\public'))

Again, in your case, using the -SimpleMatch is a better solution.