Unexpected Select-String behavior
Prerequisites
- [X] Write a descriptive title.
- [X] Make sure you are able to repro it on the latest released version
- [X] Search the existing issues.
- [X] Refer to the FAQ.
- [X] Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
With version 7.2.12 (LTS) this command
winget list | Select-String -Pattern 'Winrar'
gives me highlighted result
WinRAR 6.22 (64-разрядная) RARLab.WinRAR 6.22.0 winget
while current version 7.3.5 gives me some strange symbols instead of highlighting.
[7mWinRAR[0m 6.22 (64-разрядная) RARLab.WinRAR 6.22.0 winget
I showed winget for example PS 7.3.5 does so with any external program. I used Select-String extensively while migrating from openssl 1.1.1 to 3.1.1 (openssl windows x64 slproweb). And had same behavior. It's same PC and same PS profile.
Expected behavior
Highlighted result
Actual behavior
Strange symbols
Error details
No response
Environment data
PSVersion 7.3.5
PSEdition Core
GitCommitId 7.3.5
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
I can repro this on 7.3.5 and have a few workarounds that may help though used this to search the package repo not installed packages as I don't use winget for install This is due to returning ANSI Escape sequences which you can find out more about in about_ansi_terminals help doc
Workaround 1 - Add -Raw to Select-String call
winget search Winrar | Select-String -Pattern 'Winrar
Adding -Raw to Select-String like this winget search Winrar | Select-String -Pattern 'Winrar -Raw returns as expected
Workaround 2 - Change $PSStyle.OutputRendering
As per the above about doc - this is something that you can control
I found setting $PSStyle.OutputRendering = 'PlainText' removed the ANSI chars from output and showed the below which is slightly different to your desired output but I think acceptable compared to current output
Workaround 3 - Use Pwsh in Windows Terminal
This then works as expected as shows properly as per the below
I'm not sure when this became an issue but will check the PR's for when this occurred and find out for you.
Hope the workarounds help for now
Thank you. I knew about -raw argument so I used exactly it. I don't use Windows Terminal but it's seems to work just fine as you clearly show. But why PS 3.5.x doesn't highlight the text in the default system console is still a question.
This is a known bug, and it affects:
Select-Stringpipelines with input from native programs (simple repro:cmd /c ver | Select-String ver) whose output prints straight to the display,- but only on Windows, and there only in regular (
conhost.exe) console windows.
See:
- #19425
* [Selective disabling/re-enabling of VT support (ANSI escape sequences) is broken #19425](https://github.com/PowerShell/PowerShell/issues/19425)
You are right - this is the same thing just another example. It's a bit strange that it doesn't affect PS 7.2 LTS.
Glad to hear it, @lelik107.
It's a bit strange that it doesn't affect PS 7.2 LTS
Indeed, the bug that was introduced in v7.3, and #19425 has some background information on what optimization may have caused it.
Please consider closing your issue, now that we know it's a duplicate of #19425.
OK, i'm closing the issue as a dublicate of https://github.com/PowerShell/PowerShell/issues/19425