Skip to content

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

@native-api

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions