Matt February 2016

Prevent trailing newline in Powershell Out-File command

How do I prevent Powershell's Out-File command from appending a newline after the text it outputs?

For example, running the following command

"TestTest" | Out-File -encoding ascii test.txt

Produces a file with contents "TestTest\r\n" rather than just "TestTest"

Answers


briantist February 2016

In PowerShell 5.0+, you would use:

"TestTest" | Out-File -encoding ascii test.txt -NoNewline

But in earlier versions you simply can't with that cmdlet.

Try this:

[System.IO.File]::WriteAllText($FilePath,"TestTest",[System.Text.Encoding]::ASCII)

Post Status

Asked in February 2016
Viewed 1,654 times
Voted 13
Answered 1 times

Search




Leave an answer