Select-string escape character

rob picture rob · Feb 5, 2015 · Viewed 12.3k times · Source

I'm trying to search directory c:\bats\ for batch files containing the unc path \\server\public

Command:

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

I receive an error related to the string \\server\public:

Select-string : The string \\server\public is not a valid regular
expression: parsing "\\server\public" - Malformed \p{X} character
escape. At line:1 char:91
+ ... ts" -recurse | Select-string -pattern \\server\public
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Select-String], ArgumentException
+ FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand

I've tried using various escapes such as "\server\public" or "'\server\public'" but I always receive that same error.

Answer

campbell.rw picture campbell.rw · Feb 5, 2015

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

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