Skip to content

The -NoNewLine switch parameter of Out-File and Out-String should not strip embedded newlines from formatter-generated output  #5107

Closed

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.txt

Note how the first input object uses the PS formatting system, whereas the second one doesn't.

Actual behavior:

one---  1two
three    # no trailing newline

Note 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 newline

You 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

  1. added
    Issue-Discussion
    on Oct 13, 2017
  2. iSazonov commented on Oct 13, 2017

    @iSazonov
    Collaborator

    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.

    ">>>", $(Get-Date), "<<<" | Out-String -NoNewLine
  3. mklement0 commented on Oct 13, 2017

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov):

    The output of Get-Date is 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-File at all is the express desire to use the formatting system (though in the case of Out-File that is obscured if you use >).

  4. iSazonov commented on Oct 14, 2017

    @iSazonov
    Collaborator

    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.

  5. mklement0 commented on Oct 15, 2017

    @mklement0
    ContributorAuthor

    I agree that this very little utility in -NoNewline for both Out-File and Out-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 -NoNewline to Out-File to begin with.

    I also wonder if the original intent for -NoNewline was 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.

  6. mklement0 commented on Oct 15, 2017

    @mklement0
    ContributorAuthor

    (Incidentally, I had the actual vs. expected behavior accidentally swapped in the original post - fixed.)

  7. added
    Resolution-Won't Fix
    on Oct 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-Won't FixWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions