How to ignore escape sequences stored in PowerShell string variable? How to ignore escape sequences stored in PowerShell string variable? powershell powershell

How to ignore escape sequences stored in PowerShell string variable?


Use the static escape() method, it instructs the regular expression engine to interpret these characters literally rather than as metacharacters:

$id = [regex]::escape($id)

You can also turn the command to a one liner (-path can take a collection of files):

Select-String -Path path\to\files\\* -Pattern ([regex]::escape($id)) -Quiet


Select-String has a -SimpleMatch parameter that will cause the cmdlet to do simple string matches instead of regular expressions. If you change the script to do:

 $found = Select-String -Path $file $id -Quiet -SimpleMatch

it should work as desired.


If the $id string already contains something like TAB when it's passed to you then I'm not aware of a built in method to safely escape it back to "\t". You need to make sure your script is passed the correct string in the first place. I.e. it needs to passed 0x5C74 (\t) not 0x09 (TAB). So the escaping needs to be done when the the search string is first defined.

Regex.Escape will escape TAB -> \t but will also escape any of these characters that have meaning within regular expressions:

\, *, +, ?, |, {, [, (,), ^, $,., #, and white space

e.g.   . -> \.