1

If I replace just the text there is no problem but the problem occurs when I replace the text that includes backslashes that is when the error occurs. How can I approach this to accept the new parameter? Here is what I have:

(Get-Content 'C:\TEST.txt' -Raw) -replace 'Log', 'Log\400\Department3' |
  Set-Content -Path 'C:\TEST.txt'
mklement0
  • 382,024
  • 64
  • 607
  • 775
Sam
  • 33
  • 4
  • 1
    In short: PowerShell's [`-replace`](https://stackoverflow.com/a/40683667/45375) _operator_ is _regex_-based and case-_insensitive_ by default. To perform _literal_ replacement, `\ `-escape metacharacters in the pattern or call `[regex]::Escape()`. By contrast, the `[string]` type's [`.Replace()`](https://learn.microsoft.com/en-US/dotnet/api/System.String.Replace) _method_ performs _literal_ replacement and is case-_sensitive_, _invariably_ in Windows PowerShell, _by default_ in PowerShell (Core). See the [answer to the linked duplicate](https://stackoverflow.com/a/64771432/45375). – mklement0 May 03 '21 at 01:17
  • I'm glad to hear it was helpful, @Sam. – mklement0 May 03 '21 at 13:39

0 Answers0