PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

Write-Host redirection does not respect -NoNewLine parameter

Open pperrier27 opened this issue 8 months ago • 1 comments

Prerequisites

Steps to reproduce

The -NoNewLine parameter on Write-Host is not being respected when the Information stream is redirected.

Possibly related to the lack of -NoNewLine on Write-Information and friends #17618, #5579

Expected behavior

In file test.ps1:
Write-Host -NoNewLine "Hello"
Write-Host "World"

PS C:\Users> .\test.ps1
HelloWorld

PS C:\Users> .\test.ps1 6>&1
HelloWorld

Actual behavior

In file test.ps1:
Write-Host -NoNewLine "Hello"
Write-Host "World"

PS C:\Users> .\test.ps1
HelloWorld

PS C:\Users> .\test.ps1 6>&1
Hello
World

Error details


Environment data

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

Visuals

No response

pperrier27 avatar Apr 24 '25 22:04 pperrier27

What seems to be happening currently:

  • What Write-Host (and Write-Information) outputs are System.Management.Automation.InformationRecord instances. For Write-Host output, these instances do store original formatting instructions such as -ForegroundColor and -NoNewLine, in the .MessageData property.

  • When redirected via the success output stream (6>&1) and printed to the host or redirected to a file (6>out.txt), PowerShell's for-display formatting system currently represents them by their .ToString() value (or possibly, equivalently, by their .MessageData.Message value), which merely prints the original text, without trying to recreate the Write-Host formatting; simple repro: Write-Host -ForegroundColor Green Green 6>&1

In other words: The formatting system could recreate the original formatting, but currently doesn't

mklement0 avatar Apr 25 '25 19:04 mklement0