Skip to content

7.4.0 File redirection with native executable no longer appends new line #20811

@twinter-amosfivesix

Description

@twinter-amosfivesix

Prerequisites

Steps to reproduce

According to about_Redirection:

Using the redirection operator with a file target is functionally equivalent to piping to Out-File with no extra parameters.

The documentation for Out-File -NoNewLine says:

No newline is added after the last output string.

Which means Out-File does normally add a newline. And this is what you see in PowerShell 7.3.10. If the output does not end in a newline, Out-File and file redirection add a newline. But in PowerShell 7.4.0, with file redirection that newline is no longer added.

To make this easy to repro you'll want a native executable that outputs character data but no newline (LF or CR/LF) at the end. Here's a C# program that does that. You don't have to use this program to repro the problem. It just helps.

namespace Example;

internal class Program
{
    static void Main()
    {
        Console.Write("Hello"); // Note that it's not WriteLine()
    }
}

This appears to be related to the PR: #17857 by @SeeminglyScience that was added in preview 4

Appears to be related to #20747 and #20808.

Expected behavior

# See "Steps to reproduce" for source code for Hello.exe

PS> .\Hello.exe >x.txt

PS> (Get-Content x.txt -Raw).EndsWith([System.Environment]::NewLine)
True

Actual behavior

# See "Steps to reproduce" for source code for Hello.exe

PS> .\Hello.exe >x.txt

PS> (Get-Content x.txt -Raw).EndsWith([System.Environment]::NewLine)
False

Error details

N/A

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

7.3.10

image

7.4.0

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions