Title: Save pure string text to file with redirection or `Out-File` unexpectedly strip off VT sequences from the original text · Issue #17452 · PowerShell/PowerShell · GitHub
Open Graph Title: Save pure string text to file with redirection or `Out-File` unexpectedly strip off VT sequences from the original text · Issue #17452 · PowerShell/PowerShell
X Title: Save pure string text to file with redirection or `Out-File` unexpectedly strip off VT sequences from the original text · Issue #17452 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/17452
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Save pure string text to file with redirection or `Out-File` unexpectedly strip off VT sequences from the original text","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://docs.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\nThis happens in 7.3.0-preview.4, and all other 7.3 preview versions.\r\n**This is a regression from both 7.0.x and 7.2.x (both are LTS versions).**\r\n\r\nContent of the test file `try.ps1`: (Ansi escape sequence is used for color).\r\n```\r\nRead-Host -Prompt \"\u001b[33mEnter your device code\u001b[0m\"\r\n```\r\n\r\nTry replacing ``\"`r`n\"`` with ``\"`n\"`` for the content in the file and save it to another file:\r\n```pwsh\r\n(Get-Content .\\try.ps1 -Raw).Replace(\"`r`n\", \"`n\") > new.ps1\r\n## Or, (Get-Content .\\try.ps1 -Raw).Replace(\"`r`n\", \"`n\") | Out-File new.ps1\r\n```\r\n\r\nNow all Ansi escape sequences are lost in the content of `new.ps1`:\r\n```\r\nRead-Host -Prompt \"Enter your device code\"\r\n```\r\n---------------------------------------------------------------\r\n\r\n`Set-Content` works as expected, which preserves the Ansi escape sequences.\r\n\r\n```pwsh\r\n(Get-Content .\\try.ps1 -Raw).Replace(\"`r`n\", \"`n\") | Set-Content new.ps1\r\n```\r\n---------------------------------------------------------------\r\n\r\nBehavior in 7.0.11:\r\n\r\n\r\n\r\nBehavior in 7.2.2:\r\n\r\n\r\n\r\n-------------------------------------------\r\n\r\n### Root cause and behavior proposal\r\n\r\nThe root cause of this regression is having `Host` to be the default value for `$PSStyle.OutputRendering`, which make the `Out-*` cmdlet to strip off escape sequences from the passed-in input.\r\n\r\n- In 7.0.x, the `$PSStyle` feature is not there, so `Out-*` commands don't do anything special to Ansi escape sequences in the input.\r\n- In 7.2.x, the default value for `$PSStyle.OutputRendering` is `Ansi`, so the behavior is unchanged without a user knowing about the `$PSStyle` feature.\r\n- In 7.3, the default value is changed to `Host`, which makes `Out-*` cmdlets to always strip off escape sequences from the input, and hence the regression.\r\n\r\nI understand the purpose of having `Host` option for `$PSStyle.OutputRendering` -- it's nice to have ANSI decorations in the formatting output when it's rendered on terminal, but when redirecting the formatting out like in `Get-Process > file`, the escape sequences in the formatting output becomes annoying. The `Host` option is to tell PowerShell to not keep ANSI decorations in case of redirection (essentially `Out-File`, and basically any `Out-*` cmdlets such as `Out-String`).\r\n\r\nBut for pure string input that doesn't really go through formatting, it doesn't seem right to remove the Ansi escape sequences, given that the formatting system doesn't add any to it. The `Out-*` should treat the pure string input as is, and only remove escape sequences when the input object actually goes through formatting.\r\n\r\nSo, the **proposal** is: option `Host` makes `Out-*` cmdlets remove escape sequences only if the input object actually goes through formatting within the `Out-*` cmdlets. For string input, `Out-*` cmdlet should not do anything special regarding Ansi escape sequences.\r\n\r\n### Expected behavior\r\n\r\n```console\r\nThe content in new.ps1 should be the same as in try.ps1, except that all \"\\r\\n\" characters are replaced with \"\\n\".\r\n```\r\n\r\n\r\n### Actual behavior\r\n\r\n```console\r\nThe escape sequences in the original file was removed during redirection.\r\n```\r\n\r\n\r\n### Error details\r\n\r\n```console\r\nN/A\r\n```\r\n\r\n\r\n### Environment data\r\n\r\n```powershell\r\nPS:24> $PSVersionTable\r\n\r\nName Value\r\n---- -----\r\nPSVersion 7.3.0-preview.4\r\nPSEdition Core\r\nGitCommitId 7.3.0-preview.4\r\nOS Microsoft Windows 10.0.19044\r\nPlatform Win32NT\r\nPSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}\r\nPSRemotingProtocolVersion 2.3\r\nSerializationVersion 1.1.0.1\r\nWSManStackVersion 3.0\r\n```\r\n\r\n\r\n### Visuals\r\n\r\n_No response_","author":{"url":"https://github.com/daxian-dbw","@type":"Person","name":"daxian-dbw"},"datePublished":"2022-05-27T20:13:35Z","url":"https://github.com/PowerShell/PowerShell/issues/17452"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:65e2b6fa-237a-7181-2009-108b8d17cbe9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CE3E:3080FC:86416:B8C83:6AFDA9FE |
| html-safe-nonce | ba3cf3e6013204044fa84b22ea6b3c4203b42d862da8b46f107fdb70e7688019 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRTNFOjMwODBGQzo4NjQxNjpCOEM4Mzo2QUZEQTlGRSIsInZpc2l0b3JfaWQiOiI0MzA4NzQyMTgxMzMxMjQ5NjYyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c8f5774a80ce1451bb3f4ec3ec79081da00777bdc8b9bcba86c3a99102ab5c06 |
| hovercard-subject-tag | issue:1251228366 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/17452/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d6ecdfb0c2dd30b86d342282c0866dafa65c0cac69d01a0de8bad79482d4c03b/PowerShell/PowerShell/issues/17452 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d6ecdfb0c2dd30b86d342282c0866dafa65c0cac69d01a0de8bad79482d4c03b/PowerShell/PowerShell/issues/17452 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | daxian-dbw |
| hostname | github.com |
| expected-hostname | github.com |
| None | bc86424ef34a73b386237872800e922d149d65ba907f63d6f150a2dcb8433bea |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 0ddf53eb35c87f1f8df13dbf75c3bdadbfc6e969 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width