Write-Host redirection does not respect -NoNewLine parameter
Prerequisites
- [x] Write a descriptive title.
- [x] Make sure you are able to repro it on the latest released version
- [x] Search the existing issues.
- [x] Refer to the FAQ.
- [x] Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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
What seems to be happening currently:
-
What
Write-Host(andWrite-Information) outputs areSystem.Management.Automation.InformationRecordinstances. ForWrite-Hostoutput, these instances do store original formatting instructions such as-ForegroundColorand-NoNewLine, in the.MessageDataproperty. -
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.Messagevalue), which merely prints the original text, without trying to recreate theWrite-Hostformatting; simple repro:Write-Host -ForegroundColor Green Green 6>&1
In other words: The formatting system could recreate the original formatting, but currently doesn't