Prevent Powershell from adding new line at the end of file


I needed to replace HTML entities in the files within several subfolders, so I used PowerShell script suggested here:

However, that script adds an extra new line to the end of file and I’d like to avoid that. There was another script listed in the very next comment in that thread ( which was supposed to achieve exactly what I need, but it didn’t work when I tried running it.

Here is my script:

All I need to do is for it NOT to be adding a new line at the end.

Thank you in advance!


PowerShell v5+ supports the -NoNewline switch with the Set-Content cmdlet (and also Add-Content and Out-File).

If you’re running an earlier version, you must use the .NET Framework directly, as demonstrated in one of the answers you link to.

Caveat: -NoNewline doesn’t just mean that a trailing newline is omitted, but that all input objects are concatenated directly, without a separator (and without adding a trailing newline).
If your input is a single multi-line string, as below, -NoNewLine will work as expected, but if you have an array of strings you want to write with newlines only between them and not also a trailing one, you’ll have to do something like:
(('one', 'two', 'three') -join "n") + "n" | Set-Content -NoNewLine $filePath).
See also: this answer of mine.

As an aside: There’s no need for multiple ForEach-Object calls or even a foreach statement; you can do it all in one pipeline (PSv3+, due to Get-Content -Raw, but you can omit -Raw to make it work (less efficiently) in PSv2 too):

Optional reading:

TheMadTechnician points out that an alternative to defining variable $filePath to refer to the full path of the input file at hand inside the script block of the ForEach-Object call is to use common parameter -PipelineVariable (-pv):

Note how the argument passed to PipelinVariable must not have the $ prefix, because it is the name of the variable to bind.
$ThisFile then refers to Get-ChildItem‘s current output object in all subsequent pipeline segment(s).

While there’s not much to be gained in this particular case, the general advantage of using
-PipelinVariable is that the variable bound this way can be referenced in any subsequent pipeline segment.


Prevent Powershell from adding new line at the end of file by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply