0

I have an ini file that looks like this:

[list_text]                       
text_002=L-Win+3=Regards\nThomas

I try to find and replace the "\nThomas" with a different name:

$settings = Get-Content -Raw $path -Encoding UTF8
$settings = $settings -replace '`r`nThomas', '\nMike'

I tested different ways trying to find the "\nThomas" but nothing seems to work.

2
  • 2
    You're looking for a literal \: -replace '\\nThomas', '\nMike' Commented Aug 30, 2022 at 11:57
  • thanks it is working! If you paste this as answer I can mark it correctly. Commented Aug 30, 2022 at 12:00

1 Answer 1

3

The pattern "`r`n..." will look for a literal carriage return and newline characters.

You aren't looking for any of those, you're looking for the verbatim escape sequence \n. To describe a backslash in a regex pattern, escape it with another backslash:

$settings -replace '\\nThomas', '\nMike'

You can also use [regex]::Escape() to escape any given verbatim string:

$settings -replace ([regex]::Escape('\nThomas')), '\nMike'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.