Prevent trailing newline in PowerShell Out-File command

大城市里の小女人 提交于 2019-11-27 17:13:30

问题


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

For example, running the following command produces a file with contents "TestTest\r\n" rather than just "TestTest".

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

回答1:


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)



回答2:


To complement briantist's helpful answer re -NoNewline:

The following applies not just to Out-File, but analogously to Set-Content / Add-Content as well; as stated, -NoNewline requires PSv5+.

Note that -NoNewline means that with multiple objects to output, it is not just a trailing newline (line break) that is suppressed, but any newlines.

In other words: The string representations of the input objects are directly concatenated, without a separator (terminator).

Therefore, the following commands result in the same file contents (TestTest without a trailing newline):

# Single input string
"TestTest" | Out-File -encoding ascii test.txt -NoNewline

# Equivalent command: 2-element array of strings that are directly concatenated.
"Test", "Test" | Out-File -encoding ascii test.txt -NoNewline

In order to place newlines only between, but not also after the output objects, you must join the objects with newlines explicitly:

"Test", "Test" -join "`r`n" | Out-File -encoding ascii test.txt -NoNewline

To create Unix-style LF-only newlines, use "`n" instead of "`r`n".

Caveat:

The above -join solution implicitly converts the input objects to strings, if they aren't already and does so by calling the .NET .ToString() method on each object, which often yields a different representation than the one that Out-File would directly create, because Out-File uses PowerShell's default output formatter; for instance, compare the outputs of (Get-Date).ToString() and just Get-Date.

If your input comprises only strings and/or non-strings whose .ToString() representation is satisfactory, the above solution works, but note that it is then generally preferable to use the Set-Content cmdlet, which applies the same stringification implicitly.
For a complete discussion of the differences between Out-File and Set-Content, see this answer of mine.

If your input has non-strings that do you want to be formatted as they would print to the console, there is actually no simple solution: while you can use Out-String to create per-object string representations with the default formatter, Out-String's lack of -NoNewline (as of v5.1; this GitHub issue suggests introducing it) would invariably yield trailing newlines.



来源:https://stackoverflow.com/questions/35279848/prevent-trailing-newline-in-powershell-out-file-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!