TinMan
TinMan

Reputation: 3

Powershell command from cmd doesnt replace line breaks

I am trying to replace a string including a line break in a file. I am using the command line for this.

I am trying to use the same command in a CMD shell and in PowerShell, however I can only seem to get it to work in the latter.

Here is the command:

powershell -Command "(Get-Content client.properties -Raw).Replace('#test`r`n','test`r`n') | Set-Content client2.properties"

Why is this not working in a CMD shell, and how do I make it work?

Upvotes: 0

Views: 304

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174505

The `r`n escape sequence won't work inside single-quotes.

Use the -replace operator instead and use regex escapes:

powershell -Command "(Get-Content client.properties -Raw)-replace('#test\r?\n','test'+$([Environment]::NewLine)) | Set-Content client2.properties"

Upvotes: 2

Related Questions