• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 770
  • Last Modified:

Powershell Search with slash

The script below was created to search thru file to find occurrences. I need  
to find Instances of the search string below since I am looking for connection string in SSIS.

My issue is that the search string does not work because it has a "\" in the string. I need the search to work with the slash in my search string.

$RootString = "C:\Temp\"
$FileName = Get-ChildItem $RootString  -recurse | Select-String -pattern $SearchString | group path | select name

$FileName | Out-GridView

$FileName | Out-file -width 300 -filepath "C:\Users\ltorres\Desktop\pshellOutPut_SP3.txt"

Open in new window

Leo Torres
Leo Torres
3 Solutions
why not just escape it:
QlemoBatchelor and DeveloperCommented:
Or use -SimpleMatch with select-string to switch off RegEx evaluation.

$FileName = Get-ChildItem $RootString  -recurse | Select-String -pattern $SearchString –simplematch | group path | select name
Fernando SotoRetiredCommented:
Hi ltorres321;

In Windows PowerShell an expression with a \ is interpreted as the start of a regular expression. So that PowerShell does not interpret thee \ character to be a regular expression escape it as @becraig has suggested by doubling up on the character as so \\.

The PowerShell the escape character is the grave-accent(`) . For example the following `n = new line, `r = carriage return, `t = horizontal tab and so on.
Leo TorresSQL DeveloperAuthor Commented:
I didn't know I could use \\.. But learning simple match will be nice.. can you show a quick sample
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now