PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

Unexpected Select-String behavior

Open lelik107 opened this issue 3 years ago • 2 comments

Prerequisites

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.

WinRAR 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

PS7 2 12 PS7 3 5

lelik107 avatar Jun 28 '23 07:06 lelik107

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

image

Workaround 3 - Use Pwsh in Windows Terminal

This then works as expected as shows properly as per the below image

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

kilasuit avatar Jul 02 '23 19:07 kilasuit

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.

lelik107 avatar Jul 02 '23 20:07 lelik107

This is a known bug, and it affects:

  • Select-String pipelines 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

mklement0 avatar Jul 25 '23 17:07 mklement0

* [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.

lelik107 avatar Jul 26 '23 05:07 lelik107

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.

mklement0 avatar Jul 26 '23 15:07 mklement0

OK, i'm closing the issue as a dublicate of https://github.com/PowerShell/PowerShell/issues/19425

lelik107 avatar Jul 26 '23 15:07 lelik107