Out-String should not append a trailing newline
See original GitHub issueSummary of the new feature/enhancement
It should be possible to use Out-String
in a manner that doesn’t append a trailing newline, which is what it currently does and has always done.
Note that -NoNewLine
is not a solution, because it suppresses all newlines, so that two or more output lines are then simply directly concatenated (like -join ''
; e.g. 1, 2 | Out-String -NoNewLine
yields 12
).
Here’s a demonstration of the problem:
# CURRENT behavior:
PS> ('foo' | Out-String) -replace '\r?\n', '<newline>'
foo<newline> # A newline was appended.
I see two options:
-
(a) Consider a breaking change - assuming it is justified to consider it a Bucket 3: Unlikely Grey Area change - and make
Out-String
never append a trailing newline. -
(b) Introduce a new switch,
-NoTrailingNewLine
(related: #5108)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:16
- Comments:39 (17 by maintainers)
Top Results From Across the Web
How to prevent trailing newline in PowerShell script?
2. Use something like (Get-ComputerInfo -Property $LST1 | Format-List | Out-String).Trim() | Add-Content -Path $path -NoNewline. – Theo. May 15, ...
Read more >new line not append instead append on the end of the line
This will add a new line only when there is not already one at the end. ... -n do not output the trailing...
Read more >Out-String (Microsoft.PowerShell.Utility)
By default, Out-String outputs a single string formatted as you would see it in the console including any blank headers or trailing newlines....
Read more >Adding a Newline Character to a String in Java
Learn about newline characters in Java for text output and HTML.
Read more >Guide on How to Add a New Line in JavaScript
Discover the concept of adding a new line in JavaScript. Read on to know the two approaches to add a new line that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The purpose of
Out-String
is to render objects as presentable text which includes trailing newlines. If what you actually want to do is join a bunch of strings then use-join
because that’s it for.I think maybe mklement0 chose
Get-Date
as an example because it produces a simple object that isn’t a string (aDateTime
), so thatOut-String
would be doing “some work” to convert something to a string.But consider even just a plain string!
versus
How would one could consider that
'asdf'
is not “rendered as presentable text”? In other words, who would consider the output of the latter to be “presentable text”, but not the former?Adding newlines in is ONLY interesting where there are multiple input objects: newlines in that case are used to separate them (unless belayed by
-NoNewline
). The current last (trailing) newline is separating the last object from … what? It serves no purpose; it is not separating it from anything. We should get rid of it.Consider also this pathological scenario:
Let’s stop the madness and pull this burr out from under the saddle.
(edit: I called that last scenario “pathological”, because nobody is going to pipe through a long chain of
Out-String
commands, but I think the nerve it hits is quite reasonable: it feels like it ought to be idempotent)