Write-Host prints ANSI control characters when running script from stdin even if stdout is redirected · Issue #26631 · PowerShell/PowerShell · GitHub
Skip to content

Write-Host prints ANSI control characters when running script from stdin even if stdout is redirected #26631

Open

Description

@native-api

Prerequisites

Steps to reproduce

  • Run a script from a Here-Document with output to a file (or a pipe etc), in Ubuntu Terminal:
$ pwsh -c - >out.txt <<!
> echo \$Env:PATH
> !
$ xxd out.txt 
00000000: 1b5b 3f31 681b 5b3f 316c 2f6f 7074 2f6d  .[?1h.[?1l/opt/m
00000010: 6963 726f 736f 6674 2f70 6f77 6572 7368  icrosoft/powersh
00000020: 656c 6c2f 373a 2f68 6f6d 652f 766d 7573  ell/7:/home/vmus
00000030: 6572 2f2e 7069 7869 2f62 696e 3a2f 686f  er/.pixi/bin:/ho
<...>
00000150: 733a 2f73 6e61 702f 6269 6e3a 2f73 6e61  s:/snap/bin:/sna
00000160: 702f 6269 6e0a 1b5b 3f31 68              p/bin..[?1h
  • Notably, if you run the same script from a command line argument, the output works correctly:
$ pwsh -c 'echo $Env:PATH' >out.txt
vmuser@ubuntuvm:~/.pyenv$ xxd out.txt 
00000000: 2f6f 7074 2f6d 6963 726f 736f 6674 2f70  /opt/microsoft/p
00000010: 6f77 6572 7368 656c 6c2f 373a 2f68 6f6d  owershell/7:/hom
<..>
00000140: 6c2f 6761 6d65 733a 2f73 6e61 702f 6269  l/games:/snap/bi
00000150: 6e3a 2f73 6e61 702f 6269 6e0a            n:/snap/bin.

Expected behavior

With $PsStyle.OutputProcessing="Host", ANSI escape sequences should be written to a stream only if it points to a terminal (isatty returns 1).

This is the default behavior of standard UNIX console programs that can print ANSI escape sequences.

Actual behavior

(see above in reproduction steps)

  • Whether stderr is also redirected does not affect the behavior

Error details

N/A

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.4
PSEdition                      Core
GitCommitId                    7.5.4
OS                             Ubuntu 24.04.3 LTS
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Dec 20, 2025
  2. added
    Issue-BugIssue has been identified as a bug in the product
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Oct 5, 2026
  3. SeeminglyScience commented on Oct 5, 2026

    @SeeminglyScience
    Contributor

    The Engine WG discussed this, we agree that the host should not be emitting these VT sequences when processing stdin as commands.

    For reference:

    Here's where they are defined in the code.

    Here's the PR where it was added.

  4. added
    WG-ReviewedA Working Group has reviewed this and made a recommendation
    WG-TriagedThe issue has been triaged by the designated WG.
    and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    WG-NeedsReviewNeeds a review by the labeled Working Group
    WG-Enginecore PowerShell engine, interpreter, and runtime
    on Oct 5, 2026
  5. candygoogy commented on Oct 11, 2026

    @candygoogy

    I'm working on this: not emitting those VT sequences when commands come from stdin, while keeping the interactive behaviour from #14943. @PowerShell/powershell could you assign it to me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-ReviewedA Working Group has reviewed this and made a recommendationWG-TriagedThe issue has been triaged by the designated WG.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions