Skip to content

Add a -NoLastNewline switch parameter to Write-Host, Set-Content, Add-Content, Out-File, Out-String #5108

@mklement0

Description

@mklement0

Follow-up to #3684.

With multiple input objects, the -NoNewline switch causes said cmdlets to not only omit a trailing (last) newline, but also not to place newlines between the (output representations of) the input objects on output.

However, perhaps more typically, you want to only suppress a trailing newline, so it would be handy to have a -NoLastNewline parameter (mutually exclusive with -NoNewLine) that does just that.

If #3855 gets implemented, an alias -NoLastDelimiter could be defined, which would effectively turn the application of the delimiter from terminator logic (place delimiter after every item, including the last) into separator logic (place delimiter only between items).

Desired behavior

  • Out-String example:
PS> 'a', 'b' | Out-String -NoLastNewline
a
b

Note that without -NoLastNewLine you'd get a blank line at the end, due to an extraneous newline getting appended, as discussed in #14444 (which asked for -NoLastNewLine to become the default behavior, but that was declined due to backward-compatibility concerns).

  • Write-Host example:
PS> 'a', 'b' | Write-Host -NoLastNewline; Write-Host 'c'
a
bc

Note that with -NoNewline you'd get abc.

Input object-internal (embedded) newlines should be left untouched by both -NoLastNewline and
-NoNewline (though the latter doesn't do so consistently at present - see #5107).

Environment data

PowerShell Core v6.0.0-beta.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management moduleWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions