How can I prevent additional newlines with set-content while keeping existing ones when saving in UTF8?

Question:

I have a small powershell script which reads a document with UTF8 encoding, makes some replacements in it and saves it back which looks like this:

This will create a new file with the right encoding and right contents but there are additional new line characters at the end. According to this answer and many others, I am told to either:

  1. Add the parameter -NoNewLine to Set-Content
  2. Use [System.IO.File]::WriteAllText($path2,$content,[System.Text.Encoding]::UTF8)

Both solutions remove the trailing new lines… and every other new lines in the file.

Is there a way to both:

  1. Remove the trailing new lines while saving the file.
  2. Keep the existing new lines in my file.

Answer:

[IO.File]::WriteAllText() assumes that $content is a single string, but Get-Content produces an array of strings (and removes the line breaks from the end of each line/string). Mangling that string array into a single string joins the strings using the $OFS character (see here).

To avoid this behavior you need to ensure that $content already is a single string when it’s passed to WriteAllText(). There are various ways to do that, for instance:

  • Use Get-Content -Raw (PowerShell v3 or newer):
  • Pipe the output through Out-String:
  • Join the array with the -join operator:


Source:

How can I prevent additional newlines with set-content while keeping existing ones when saving in UTF8? by licensed under CC BY-SA | With most appropriate answer!

Note, however, that Out-String (just like Set-Content) adds a trailing line break, as was pointed out in the comments. You need to remove that with a second replacement operation.

  • Join the array with the -join operator:
  • Source:

    How can I prevent additional newlines with set-content while keeping existing ones when saving in UTF8? by licensed under CC BY-SA | With most appropriate answer!

    Leave a Reply