Description
Follow-up to #3684.
With multiple input objects, the -NoNewline switch causes said cmdlets not to place a newline after each (output representation of) the input object - in other words: the input objects' (representations) are directly concatenated.
While the usefulness of this behavior is debatable, it should work consistently with respect to how newlines embedded in input object representations are handled.
Currently,
-
formatter-generated output representations have their embedded newlines stripped
-
.ToString()generated output representations have their embedded newlines _preserved.
It is the latter behavior that should be consistently applied.
Steps to reproduce:
([pscustomobject] @{ one = 1 }), "two`nthree" | Out-File -NoNewline t.txt; Get-Content -raw t.txtNote how the first input object uses the PS formatting system, whereas the second one doesn't.
Actual behavior:
one--- 1two
three # no trailing newlineNote how the internal newlines were stripped from the default output formatting for @{ one = 1 }, which is:
one
---
1
Expected behavior:
one
---
1two
three # no trailing newlineYou could argue that -NoNewline with formatter-generated output doesn't make much sense with multiple input objects, and the mixing with .ToString() formatted objects produces strange effects, but stripping all embedded newlines makes even less sense.
Environment data
PowerShell Core v6.0.0-beta.8
Activity
iSazonov commentedon Oct 13, 2017
I assume that the original idea was to output the original formatted objects without modification and without a new line between its. The thought was probably about simple strings, not composite objects.
mklement0 commentedon Oct 13, 2017
Ilya (@iSazonov):
The output of
Get-Dateis not a simple string, it's a formatter-generated multi-line string that happens to have only one data line, surrounded by empty lines.Have only one data line is rare, however - most formatted output has multiple non-empty lines that make no sense when run together.
Note that the only reason to use
Out-String/Out-Fileat all is the express desire to use the formatting system (though in the case ofOut-Filethat is obscured if you use>).iSazonov commentedon Oct 14, 2017
If it make no sense nobody use it :-) As for me the Issue looks like a side affect that no one can see because doesn't use.
mklement0 commentedon Oct 15, 2017
I agree that this very little utility in
-NoNewlinefor bothOut-FileandOut-String:As stated,
Out-*is only needed if you do want the formatter involved, and that's precisely the behavior that is useless with-NoNewline.In hindsight, it makes me wonder whether there was ever a good reason to add
-NoNewlinetoOut-Fileto begin with.I also wonder if the original intent for
-NoNewlinewas what we're now proposing for-NoLastNewline, and whether it was simply implemented incorrectly.I'm closing this, and will add more comments to the related doc issue I've created.
mklement0 commentedon Oct 15, 2017
(Incidentally, I had the actual vs. expected behavior accidentally swapped in the original post - fixed.)