Skip to content

Add a parameter to Out-File and Out-String to bring back the previous behavior that got changed by #17455 #17509

Closed

Description

For reference:

Context summary

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.

Activity

  1. added
    WG-Engine
    Needs-Triage
    on Jun 9, 2022
  2. daxian-dbw commented on Jun 9, 2022

    @daxian-dbw
    MemberAuthor

    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:

    1. 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.

      Get-Content file-a | Set-Content file-b
      Get-Content file-a > file-b
      
    2. 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.

    3. 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.

      Get-Content file-a | % { [System.Management.Automation.Internal.StringDecorated]::new($_).ToString("PlainText") } | Out-File file-b
      

    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.

  3. vexx32 commented on Jun 9, 2022

    @vexx32
    Collaborator

    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.

  4. SteveL-MSFT commented on Jun 9, 2022

    @SteveL-MSFT
    Member

    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.

  5. daxian-dbw commented on Jun 9, 2022

    @daxian-dbw
    MemberAuthor

    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).

  6. jhoneill commented on Jun 9, 2022

    @jhoneill

    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)

  7. daxian-dbw commented on Jun 9, 2022

    @daxian-dbw
    MemberAuthor

    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:

    1. 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.
    2. 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.
  8. michaeltlombardi commented on Jun 9, 2022

    @michaeltlombardi
    Contributor

    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)

  9. daxian-dbw commented on Jun 9, 2022

    @daxian-dbw
    MemberAuthor

    Mikey Lombardi (He/Him) (@michaeltlombardi) The default behavior of Out-File and Out-String is determined. If a parameter to Out-File and Out-String is decided to be needed, we only need to update the docs of Out-String and Out-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.

    Describes the features of PowerShell that use ANSI escape sequences and the terminal hosts that support them.
  10. jhoneill commented on Jun 9, 2022

    @jhoneill

    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

  11. added
    Issue-Enhancement
    and removed
    Needs-Triage
    on Jun 9, 2022
  12. mklement0 commented on Feb 28, 2023

    @mklement0
    Contributor

    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.

  13. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  14. added
    Resolution-No Activity
    on Nov 15, 2023
  15. microsoft-github-policy-service commented on Nov 22, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions