You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The purpose of $PSStyle.OutputRendering = 'Host' (Host is the default value) is to use ANSI/VT sequences (for colored output) only when printing to the host (display), but not when command output is captured or redirected.
While this works in-session, the CLI currently always uses ANSI sequences (i.e. it behaves as if $PSStyle.OutputRendering = 'Ansi' were in effect).
Specifically, the CLI doesn't modify its behavior based on whether its stdout stream is connected to a terminal or not (which is the analog to the in-session behavior):
# On Windows, from cmd.exe
pwsh -noprofile -c $PSVersionTable| findstr .
# On Unix, from Bash, for instance
pwsh -noprofile -c '$PSVersionTable'| cat
Note:
While programmatically processing for-display formatted output via text parsing isn't recommended, it still shouldn't be made harder by including ANSI sequences in the output text.
Correcting the behavior as suggested would then require an in-session $PSStyle.OutputRendering = 'Ansi' call in the case colored output is desired when captured/redirected; ideally, there'd be a CLI parameter as an alternative, analogous to the --color=always option that GNU utilities such as ls and grep support.
I do not understand why it should be uncolored? Is this hard to set $PSStyle.OutputRendering = 'PlainText' before outputing or redirecting? This task is not very often required, in my practice in several scripts that output table to file.
When the value is Host, coloring should only be applied when outputting to the host (terminal) - it shouldn't make a difference whether output is session-internal, to the success-output stream, or session-external to stdout; e.g, pwsh -noprofile -c '$PSVersionTable | Out-String' | cat produces uncolored output.
This is not just a matter of consistency, but is sensible behavior that is typical for many external CLIs, such as the GNU utilities that come with Linux.
Your example has nothing to do with this issue, which is about conditional coloring depending on whether the success stream / stdout is connected to a host / terminal or not and its consistent application.
(Of course there are many differences between PowerShell and external utilities, but there are also commonalities, and this is one, and it should act consistently.)
Activity
237dmitry commentedon Aug 26, 2023
I do not understand why it should be uncolored? Is this hard to set
$PSStyle.OutputRendering = 'PlainText'before outputing or redirecting? This task is not very often required, in my practice in several scripts that output table to file.mklement0 commentedon Aug 26, 2023
When the value is
Host, coloring should only be applied when outputting to the host (terminal) - it shouldn't make a difference whether output is session-internal, to the success-output stream, or session-external to stdout; e.g,pwsh -noprofile -c '$PSVersionTable | Out-String' | catproduces uncolored output.This is not just a matter of consistency, but is sensible behavior that is typical for many external CLIs, such as the GNU utilities that come with Linux.
237dmitry commentedon Aug 26, 2023
There are many other examples of different behavior.
For example
Out-Stringoutputs single string of lines and pwsh understand it as is, but native utilities do not understand multiline string:mklement0 commentedon Aug 26, 2023
Your example has nothing to do with this issue, which is about conditional coloring depending on whether the success stream / stdout is connected to a host / terminal or not and its consistent application.
(Of course there are many differences between PowerShell and external utilities, but there are also commonalities, and this is one, and it should act consistently.)
237dmitry commentedon Aug 26, 2023
I think
$PSStyle.OutputRenderingworks properly. But I do not understand when ))PS. And I think Write-Host shouldn't output any data to the pipe at all. As it says in the help:
mklement0 commentedon Aug 28, 2023
All the in-session behaviors you're showing are as designed.
By contrast, this issue is about the behavior of the CLI (and, with a
*>&1or6>&1redirection in-session, also the behavior ofOut-String):Indeed it shouldn't and it doesn't.
However, in a CLI call,
Write-Hostoutput is sent to stdout.pwsh -c "Write-Host foo"implicitly behaves likepwsh -c "Write-Host foo *>&1"