question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Out-String should not append a trailing newline

See original GitHub issue

Summary 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:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:39 (17 by maintainers)

github_iconTop GitHub Comments

12reactions
bpayettecommented, Dec 19, 2020

there’s no good reason to append a newline to the (formatted representation) of the last input object.

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.

8reactions
jazzdelightsmecommented, Jan 18, 2021

I think maybe mklement0 chose Get-Date as an example because it produces a simple object that isn’t a string (a DateTime), so that Out-String would be doing “some work” to convert something to a string.

But consider even just a plain string!

'asdf'

versus

'asdf' | Out-String

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:

'asdf' | Out-String | Out-String | Out-String | Out-String | Out-String 

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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found