1

I'm trying to accomplish a search and replace in multiple files. See sample code below. But when I use the characters [] , :: , / it replaces multiple times all over the place. Is there a way around this?

PS C:"Some_Path"> $newFiles = Get-ChildItem . *.par -rec 
foreach ($file in $newFiles)
{(Get-Content $file.PSPath) | Foreach-Object { $_ -replace "[text]", " /Text/Text_Data1::[404PC001]" } | Set-Content $file.PSPath }

Thanks

.par is a text file

reyPanda
  • 510
  • 2
  • 11

1 Answers1

0

The -replace operator in PowerShell uses Regular Expressions. You need to quote characters that are special in these expressions. To do this in your example, add '\' before the '[' and ']' characters as in:

$_ -replace "\[text\]", " /Text/Text_Data1::[404PC001]" 
Bruce Payette
  • 2,511
  • 10
  • 8