Skip to content

out-file producing weird characters #16730

Closed

Description

@computechrr

Prerequisites

Steps to reproduce

When using out-file with or without specifying any encoding the file has some weird characters at the beginning of the file

�[32;1m NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName�[0m
�[32;1m ------ ----- ----- ------ -- -- -----------�[0m

I've tried this on different computers all using ps 7.2.1, windows 10, server 2016 etc. When using the same get-process |out-file with powershell 5.1 it works as expected without those characters.

Expected behavior

NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName

Actual behavior

�[32;1m NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName�[0m

Error details

No response

Environment data

7.2.1

Visuals

image

Activity

  1. added
    Needs-Triage
    on Jan 11, 2022
  2. vexx32 commented on Jan 11, 2022

    @vexx32
    Collaborator
  3. 237dmitry commented on Jan 11, 2022

    @237dmitry
    & {                                
         $PSStyle.OutputRendering = 'Host'    # or PlainText
         Get-Process | Out-File gps.txt
         $PSStyle.OutputRendering = 'Ansi'
      }
    
  4. computechrr commented on Jan 11, 2022

    @computechrr
    Author
  5. computechrr commented on Jan 11, 2022

    @computechrr
    Author
  6. LeeHolmes commented on Jan 27, 2022

    @LeeHolmes
    Contributor

    This is a pretty major breaking change. This doesn't happen with other ANSI shells, such as Bash:

    image

    image

  7. SteveL-MSFT commented on Jan 27, 2022

    @SteveL-MSFT
    Member

    This is because ls itself knows when it is being redirected and omits the VT. PS7.2 handles this at the shell level due to things like formatting which happens within the PS engine. If you run ls -G > out.txt within PS7.2, you'll still see that VT is omitted because ls decided to omit it.

    I think the way to address this issue while addressing PowerShell/Announcements#28 is to change the current regex for removing VT to have a fixed list of known VT codes that will be removed. This would still need to go through a security review to ensure spoofing doesn't happen.

  8. gwojan commented on Jan 27, 2022

    @gwojan

    Steve Lee (@SteveL-MSFT) this is particularly annoying when $PSStyle is doing full filename coloring and piping the output to clip.exe. It seems like I'm doing that with increasing frequency and pasting into Teams... 😔

  9. ghost added
    In-PR
    on Jan 27, 2022
  10. ghost removed
    Needs-Triage
    on Mar 25, 2022
  11. ghost removed
    In-PR
    on Mar 25, 2022
  12. ghost added
    Resolution-Fixed
    on Mar 25, 2022
  13. JohnMarkHowell commented on May 30, 2022

    @JohnMarkHowell

    Still happening in v7.4.2

    Any suggestions other than always using a replace filter before the Out-File ?

  14. JasonDTX commented on Jul 28, 2022

    @JasonDTX

    You may find some success using ascii as the default encoding for the entire session.

    $OutputEncoding = [Text.Encoding]::ASCII

    Sorry if this was already tried, it helped with my out-file encoding in pwsh 7

  15. JohnMarkHowell commented on Jul 28, 2022

    @JohnMarkHowell

    GitHub_User_5678309 (@JasonDTX) - I just tried that and it did not alter the output (meaning the output still included escape characters).

  16. JohnMarkHowell commented on Jul 28, 2022

    @JohnMarkHowell

    Steve Lee (@SteveL-MSFT), Paul Higinbotham (@PaulHigin) - This still happens in 7.2.5 on Windows 11

  17. JasonDTX commented on Jul 28, 2022

    @JasonDTX

    JohnMarkHowell Sorry, I pasted an older attempt! Too many tabs open. This one is the one that fixed me.
    $PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::PlainText
    Out-File takes the format of the input object, so this strips any escape sequences out of my psobjects.

  18. JohnMarkHowell commented on Jul 28, 2022

    @JohnMarkHowell

    That is awesome, GitHub_User_5678309 (@JasonDTX) ! That fixed it! Thank you!!!!!!!

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions