KEMBAR78
`Write-Host` `-ForegroundColor` / `-BackgroundColor` don't respect in-session `$PSStyle.OutputRendering = 'PlainText'`, respect it via `NO_COLOR` only on Unix · Issue #21168 · PowerShell/PowerShell · GitHub
Skip to content

Write-Host -ForegroundColor / -BackgroundColor don't respect in-session $PSStyle.OutputRendering = 'PlainText', respect it via NO_COLOR only on Unix #21168

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

Note:

  • I don't know whether the intent is for Write-Host -Fore/BackgroundColor to respect $PSStyle.OutputRendering = 'PlainText' or not - arguably, it should.

    • Update: Arguably, it should respect all modes, so that 'Host' can be used to use color only when printing to the terminal, and 'ANSI' to request unconditional coloring.
  • De facto, it currently isn't respected - ever - on Windows, and only situationally on Unix-like platforms

& {
    # A preexisting NO_COLOR environment variable sets `$PSStyle.OutputRendering = 'PlainText'` in the session
    # created by the CLI call.
    $env:NO_COLOR = 1
    pwsh -noprofile -c 'Write-Host -ForegroundColor Blue hi'
    $env:NO_COLOR = $null
}
& {
    # Set  `$PSStyle.OutputRendering = 'PlainText'` in-session
    $prev = $PSStyle.OutputRendering
    $PSStyle.OutputRendering = 'PlainText'
    Write-Host -ForegroundColor Blue hi
    $PSStyle.OutputRendering = $prev
}

Expected behavior

Either both 'hi' instances should be blue (as currently on Windows), or, preferably, both should use the default foreground color.

Actual behavior

  • On Windows, both 'hi' instances are blue.
  • On Unix-like platforms, the first 'hi' uses the default color, and the second one is blue.

Error details

No response

Environment data

PowerShell 7.4.1

Visuals

No response

Metadata

Metadata

Assignees

Labels

In-PRIndicates that a PR is out for the issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions