Repository navigation
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
This happens in 7.3.0-preview.4, and all other 7.3 preview versions.
This is a regression from both 7.0.x and 7.2.x (both are LTS versions).
Content of the test file try.ps1: (Ansi escape sequence is used for color).
Read-Host -Prompt "�[33mEnter your device code�[0m"
Try replacing "`r`n" with "`n" for the content in the file and save it to another file:
(Get-Content .\try.ps1 -Raw).Replace("`r`n", "`n") > new.ps1
## Or, (Get-Content .\try.ps1 -Raw).Replace("`r`n", "`n") | Out-File new.ps1Now all Ansi escape sequences are lost in the content of new.ps1:
Read-Host -Prompt "Enter your device code"
Set-Content works as expected, which preserves the Ansi escape sequences.
(Get-Content .\try.ps1 -Raw).Replace("`r`n", "`n") | Set-Content new.ps1Behavior in 7.0.11:
Behavior in 7.2.2:
Root cause and behavior proposal
The root cause of this regression is having Host to be the default value for $PSStyle.OutputRendering, which make the Out-* cmdlet to strip off escape sequences from the passed-in input.
- In 7.0.x, the
$PSStylefeature is not there, soOut-*commands don't do anything special to Ansi escape sequences in the input. - In 7.2.x, the default value for
$PSStyle.OutputRenderingisAnsi, so the behavior is unchanged without a user knowing about the$PSStylefeature. - In 7.3, the default value is changed to
Host, which makesOut-*cmdlets to always strip off escape sequences from the input, and hence the regression.
I understand the purpose of having Host option for $PSStyle.OutputRendering -- it's nice to have ANSI decorations in the formatting output when it's rendered on terminal, but when redirecting the formatting out like in Get-Process > file, the escape sequences in the formatting output becomes annoying. The Host option is to tell PowerShell to not keep ANSI decorations in case of redirection (essentially Out-File, and basically any Out-* cmdlets such as Out-String).
But for pure string input that doesn't really go through formatting, it doesn't seem right to remove the Ansi escape sequences, given that the formatting system doesn't add any to it. The Out-* should treat the pure string input as is, and only remove escape sequences when the input object actually goes through formatting.
So, the proposal is: option Host makes Out-* cmdlets remove escape sequences only if the input object actually goes through formatting within the Out-* cmdlets. For string input, Out-* cmdlet should not do anything special regarding Ansi escape sequences.
Expected behavior
The content in new.ps1 should be the same as in try.ps1, except that all "\r\n" characters are replaced with "\n".Actual behavior
The escape sequences in the original file was removed during redirection.Error details
N/AEnvironment data
PS:24> $PSVersionTable
Name Value
---- -----
PSVersion 7.3.0-preview.4
PSEdition Core
GitCommitId 7.3.0-preview.4
OS Microsoft Windows 10.0.19044
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0Visuals
No response


Activity
jhoneill commentedon May 28, 2022
7.2.0 would have done the same thing, yes ?
The problem now is that commands are emitting ANSI corrupted data by default, e.g. even though I don't want my table headings in green and set
Formatting.TableHeaderto an empty string , I get a reset at the end of line.Anything which cleans this up so
dir > files.txtworks like it has done since the early 1980s will also "clean" things it shouldn'tIt's hard to see how Out-File can know it should strip ansi from one thing but not another.
daxian-dbw commentedon May 30, 2022
Please see #17455 for the fix to
Out-FileandOut-String.For the table header and list label styles, please see #17463 for the fix.
jhoneill commentedon May 31, 2022
#17463 will fix the extra reset so that's good.
#17455 makes the behaviour "Leave ANSI out of object formatting, but leave strings alone" which looks like a good answer. I'm sure some people will want "Strip ansi from strings" but that's a new feature and shouldn't be the default behaviour