You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a parameter to Out-File and Out-String to bring back the previous behavior that got changed by #17455 #17509
The previous behavior in 7.2.x is: Out-File and Out-String remove escape sequences from the passed-in strings when $PSStyle.OutputRendering is Host or PlainText.
There are some problems with this behavior, especially when having Host to be the default value for $PSStyle.OutputRendering. See #17455 (comment) for the details of those problems.
Therefore, that behavior was changed by #17455 to be:
when the input object is pure string, these 2 cmdlets keep the string input unchanged regardless of the RenderingOutput setting
when the input object needs to have formatting views applied to it, these 2 cmdlets remove escape sequences from the formatting output strings based on the RenderingOutput setting, which is the same as the existing behavior.
Recommendation from PowerShell Committee
The committee reviewed #17455 and recommended to add a new parameter to Out-String and Out-File to keep the previous behavior. Quote the comments #17455 (review) and #17455 (comment) below:
I think we should seriously consider adding a parameter to out-string and out-file which retains our current behavior. Perhaps a parameter -AsPlainText or some such will allow users to have the older behavior while we provide a safer default behavior.
powershell-committee reviewed this, we would recommend adding a parameter to enable users to get back the previous behavior.
I think we should wait until there is any issue reports that a user is really depending on the previous behavior, for the following reasons:
IMHO, the previous behavior is more like a bug and unexpected to users. Take the following as an example, I think most users won't expect these 2 pipelines to behave differently depending on $PSStyle.OutputRendering, since this scenario is not about output rendering at all.
Given that the previous behavior may alter the string input when $PSStyle.OutputRendering is Host or PlainText, a user may run into the spoofing issue without knowing it when these 2 output rendering options are in use. So even if we add parameter to Out-File and Out-String to bring back the previous behavior, we probably still need to warn users to be cautious using this parameter, and later on, it may become a good-practice within the community to avoid using this parameter.
If a user really want to remove Ansi escape sequences from strings, they can do it explicitly by using StringDecorated type (example below), or we can expose another cmdlet just for that. I think being explicit is better.
If we know a user is actually depending on the previous behavior, it would make more sense to add a parameter to Out-File and Out-String to bring the previous behavior back.
Reacted by Ilya, Rain Sallow (/u/ta11ow) and Patrick Meinecke
I'm in favour of exposing a cmdlet specifically for handling ANSI sequences in strings, that sounds like something that folks would find helpful as we continue to add them to formatting things.
Dongbo Wang (@daxian-dbw) the primary consideration that makes the parameter different is that it's explicitly opt-in for those cmdlets so users would be expected to understand the side-effects. So from that perspective, documentation is sufficient and a warning is not necessary.
The Committee believes there is utility to have the previous behavior where a cmdlet (like Select-String) which explicitly adds VT codes (rather than go through formatting) and the user may want those removed before having something else process it as plain text. Although you are correct that [StringDecorated] exposes this capability already, it's more dev centric and not discoverable compared to a switch on existing cmdlets.
I'm personally ok waiting on user feedback, however, before adding this.
For Select-String, the VT codes are added in formatting for the MatchInfo object. The MatchInfo.Line doesn't contain VT codes, and if you use Select-String -Raw, it output strings, without VT codes.
It's possible for a cmdlet to directly output strings with VT codes, but in that case, it's more likely the VT codes are not for decoration purpose but something the caller is supposed to consume. By contrast, it's more likely for an executable to write out VT decorated strings, where removing the VT sequences becomes interesting. But be noted that, a lot of executables are able to detect whether the stdout is redirected, and write out strings without VT codes in case of redirection, such as git.
However, for both cases, I think exposing another cmdlet just for removing VT sequences from strings is probably better, so that the operation is explicit and predictable (behavior won't change based on a global setting such as $PSStyle.OutputRendering).
I'm in favour of exposing a cmdlet specifically for handling ANSI sequences in strings, that sounds like something that folks would find helpful as we continue to add them to formatting things.
I was going to say much the same thing... but thinking about it I can see another possibility.
I think the proposal is to add a switch to both Out-String and Out-File.
To me $string | Out-String -removeAnsi would certainly work but its cumbersome (and sending strings through Out-String isn't the first thing one would think of.)
There's also probably a need for something which acts as -RemoveAnsi:($psstyle.OutputRendering -match '<whatever>')
Creating a proxy function so it can be run in the form $string | NoAnsi removes those objections. Advanced users can do that for themselves, but it would be better if it were set up out of the box : $string | NoAnsi >> filename is a lot neater than $string | out-file -append -remvoveAnsi filename (IMHO, of course)
The proxy function is an interesting idea. But I assume you mean to have Out-String/File -RemoveAnsi to always remove ANSI, regardless of what $PSStyle.OutputRendering is. If so, then that's not the previous behavior anymore, but is still something we can consider, I guess.
I think having a separate cmdlet for removing ANSI from strings would be better:
the cmdlet can have a parameter -HonorOutputRendering, so that it sorts of bring back the previous behavior by removing ANSI based on $PSStyle.OutputRendering -- no need for the cumbersome -RemoveAnsi:($psstyle.OutputRendering -match '<whatever>') then.
Out-String and Out-File all will go through a couple steppable pipelines, but the new cmdlet will simply strip off the ANSI codes and done, which would be a lot more performant.
My current thought is that if this issue won't get resolved before the next release, we'll need to document that change at least temporarily in the preview docs.
The proxy function is an interesting idea. But I assume you mean to have Out-String/File -RemoveAnsi to always remove ANSI, regardless of what $PSStyle.OutputRendering is. If so, then that's not the previous behavior anymore, but is still something we can consider, I guess.
I was thinking of two cases "always remove" and "conditionally-remove". I think always is more common, but if it a script has <external program> | <removal tool > I can see a desire for it to honour the value of outputRendering
As an aside: System.Management.Automation.Internal.StringDecorated suffers from the "pubternal" affliction - a public class in an Internal namespace. If the intent is to facilitate the class' use by end users, there should probably be a [stringdecorated] type accelerator.
microsoft-github-policy-service commented on Nov 15, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
Activity
daxian-dbw commentedon Jun 9, 2022
I think we should wait until there is any issue reports that a user is really depending on the previous behavior, for the following reasons:
IMHO, the previous behavior is more like a bug and unexpected to users. Take the following as an example, I think most users won't expect these 2 pipelines to behave differently depending on
$PSStyle.OutputRendering, since this scenario is not about output rendering at all.Given that the previous behavior may alter the string input when
$PSStyle.OutputRenderingisHostorPlainText, a user may run into the spoofing issue without knowing it when these 2 output rendering options are in use. So even if we add parameter toOut-FileandOut-Stringto bring back the previous behavior, we probably still need to warn users to be cautious using this parameter, and later on, it may become a good-practice within the community to avoid using this parameter.If a user really want to remove Ansi escape sequences from strings, they can do it explicitly by using
StringDecoratedtype (example below), or we can expose another cmdlet just for that. I think being explicit is better.If we know a user is actually depending on the previous behavior, it would make more sense to add a parameter to
Out-FileandOut-Stringto bring the previous behavior back.vexx32 commentedon Jun 9, 2022
I'm in favour of exposing a cmdlet specifically for handling ANSI sequences in strings, that sounds like something that folks would find helpful as we continue to add them to formatting things.
SteveL-MSFT commentedon Jun 9, 2022
Dongbo Wang (@daxian-dbw) the primary consideration that makes the parameter different is that it's explicitly opt-in for those cmdlets so users would be expected to understand the side-effects. So from that perspective, documentation is sufficient and a warning is not necessary.
The Committee believes there is utility to have the previous behavior where a cmdlet (like
Select-String) which explicitly adds VT codes (rather than go through formatting) and the user may want those removed before having something else process it as plain text. Although you are correct that[StringDecorated]exposes this capability already, it's more dev centric and not discoverable compared to a switch on existing cmdlets.I'm personally ok waiting on user feedback, however, before adding this.
daxian-dbw commentedon Jun 9, 2022
For
Select-String, the VT codes are added in formatting for theMatchInfoobject. TheMatchInfo.Linedoesn't contain VT codes, and if you useSelect-String -Raw, it output strings, without VT codes.It's possible for a cmdlet to directly output strings with VT codes, but in that case, it's more likely the VT codes are not for decoration purpose but something the caller is supposed to consume. By contrast, it's more likely for an executable to write out VT decorated strings, where removing the VT sequences becomes interesting. But be noted that, a lot of executables are able to detect whether the stdout is redirected, and write out strings without VT codes in case of redirection, such as
git.However, for both cases, I think exposing another cmdlet just for removing VT sequences from strings is probably better, so that the operation is explicit and predictable (behavior won't change based on a global setting such as
$PSStyle.OutputRendering).jhoneill commentedon Jun 9, 2022
I was going to say much the same thing... but thinking about it I can see another possibility.
I think the proposal is to add a switch to both
Out-StringandOut-File.To me
$string | Out-String -removeAnsiwould certainly work but its cumbersome (and sending strings throughOut-Stringisn't the first thing one would think of.)There's also probably a need for something which acts as
-RemoveAnsi:($psstyle.OutputRendering -match '<whatever>')Creating a proxy function so it can be run in the form
$string | NoAnsiremoves those objections. Advanced users can do that for themselves, but it would be better if it were set up out of the box :$string | NoAnsi >> filenameis a lot neater than$string | out-file -append -remvoveAnsi filename(IMHO, of course)daxian-dbw commentedon Jun 9, 2022
The proxy function is an interesting idea. But I assume you mean to have
Out-String/File -RemoveAnsito always remove ANSI, regardless of what$PSStyle.OutputRenderingis. If so, then that's not the previous behavior anymore, but is still something we can consider, I guess.I think having a separate cmdlet for removing ANSI from strings would be better:
-HonorOutputRendering, so that it sorts of bring back the previous behavior by removing ANSI based on$PSStyle.OutputRendering-- no need for the cumbersome-RemoveAnsi:($psstyle.OutputRendering -match '<whatever>')then.Out-StringandOut-Fileall will go through a couple steppable pipelines, but the new cmdlet will simply strip off the ANSI codes and done, which would be a lot more performant.michaeltlombardi commentedon Jun 9, 2022
Dongbo Wang (@daxian-dbw) should we hold off on MicrosoftDocs/PowerShell-Docs#8892 until there's clarity on this or go ahead with that issue in the meantime?
My current thought is that if this issue won't get resolved before the next release, we'll need to document that change at least temporarily in the preview docs.
CC Sean Wheeler (@sdwheeler)
daxian-dbw commentedon Jun 9, 2022
Mikey Lombardi (He/Him) (@michaeltlombardi) The default behavior of
Out-FileandOut-Stringis determined. If a parameter toOut-FileandOut-Stringis decided to be needed, we only need to update the docs ofOut-StringandOut-File. So, for the updates to https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ansi_terminals?view=powershell-7.2, I think we can go ahead and do it.jhoneill commentedon Jun 9, 2022
I was thinking of two cases "always remove" and "conditionally-remove". I think always is more common, but if it a script has
<external program> | <removal tool >I can see a desire for it to honour the value ofoutputRenderingmklement0 commentedon Feb 28, 2023
As an aside:
System.Management.Automation.Internal.StringDecoratedsuffers from the "pubternal" affliction - a public class in anInternalnamespace. If the intent is to facilitate the class' use by end users, there should probably be a[stringdecorated]type accelerator.microsoft-github-policy-service commentedon Nov 15, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 22, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.