-1

I have a file with some strings like below for which I want to replace the || with newline using powershell 5.1.

This is || some test || string

I've follow some advices in this thread and I'm trying with these 2 codes

(Get-Content input.txt) -replace '\|\|', '`r`n' | Out-File output.txt


(Get-Content input.txt) -replace '\|\|', '`r`n' | Set-Content output.txt -Force

but I'm getting this output

This is `r`n some test `r`n string

and desired output is

This is 
some test 
string

How to fix this?

2
  • 3
    In short: In PowerShell, only "..." strings (double-quoted aka expandable strings) perform string interpolation, i.e. expansion of variable values, expressions, and escape sequences such as `n, See the linked duplicate and the conceptual about_Quoting_Rules help topic. Commented yesterday
  • as an aside ... you can use [System.Environment]::NewLine to make the newline idea far more obvious. you can even assign it to a $Var and use that ... [grin] Commented 15 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.