Title: Preference variable $OutputEncoding in child scopes misbehaves with encodings created with New-Object / commands rather than expressions · Issue #5763 · PowerShell/PowerShell · GitHub
Open Graph Title: Preference variable $OutputEncoding in child scopes misbehaves with encodings created with New-Object / commands rather than expressions · Issue #5763 · PowerShell/PowerShell
X Title: Preference variable $OutputEncoding in child scopes misbehaves with encodings created with New-Object / commands rather than expressions · Issue #5763 · PowerShell/PowerShell
Description: Note: This issue may well be just a symptom of #5579; if so, it still may be of interest, because it demonstrate that the linked issue has real-world consequences. Preference variable $OutputEncoding determines the encoding used to send ...
Open Graph Description: Note: This issue may well be just a symptom of #5579; if so, it still may be of interest, because it demonstrate that the linked issue has real-world consequences. Preference variable $OutputEncodi...
X Description: Note: This issue may well be just a symptom of #5579; if so, it still may be of interest, because it demonstrate that the linked issue has real-world consequences. Preference variable $OutputEncodi...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/5763
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Preference variable $OutputEncoding in child scopes misbehaves with encodings created with New-Object / commands rather than expressions","articleBody":"Note: This issue may well be just a symptom of #5579; if so, it still may be of interest, because it demonstrate that the linked issue has real-world consequences.\r\n\r\nPreference variable `$OutputEncoding` determines the encoding used to send text to external utilities.\r\n\r\nIn the _global_ scope, assigning the output from a `New-Object` call or, generally, a _command_ (as opposed to an _expression_) works fine.\r\n\r\nHowever, **in a _non-global_ scope, such as inside a function, it does not: assigning output from a _command_ to `$OutputEncoding` is effectively ignored.**\r\n\r\nFor simplicity, the example below uses `Write-Output` in a somewhat contrived manner, but **an attempt to use `New-Object` to instantiate a specific encoding, e.g., `New-Object System.Text.Utf8Encoding`, would exhibit the same problem: it would be ignored.**\r\n\r\nThe **workaround** is to apply `.psobject.BaseObject` to `New-Object`'s result, or, in PSv5+, to use `[\u003cencoding\u003e]::new()` instead.\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\nOn Windows:\r\n\r\n```powershell\r\nfunction foo0 { 'ü' | findstr 'ü' }\r\nfunction foo1 { $OutputEncoding = [Console]::OutputEncoding; 'ü' | findstr 'ü' }\r\n# Same as foo1, except that Write-Output is used to produce output.\r\nfunction foo2 { $OutputEncoding = Write-Output ([Console]::OutputEncoding); 'ü' | findstr 'ü' }\r\n\r\nfoo0; '---'; foo1; '---'; foo2\r\n```\r\n\r\nExpected behavior\r\n-----------------\r\n\r\n```none\r\n---\r\nü\r\n---\r\nü\r\n```\r\n\r\nNote that `foo0`, perhaps surprisingly, does _not_ output anything, because the default `$OuputEncoding` value - UTF-8 in PowerShell Core and ASCII in Windows PowerShell - results in output that prevents `findstr.exe` from matching non-ASCII characters.\r\n\r\nActual behavior\r\n---------------\r\n\r\n```none\r\n---\r\nü\r\n---\r\n```\r\n\r\nThat is, only the _expression-based_ assignment to the (non-global) `$OutputEncoding` variable took effect, not the _command-based_ one.\r\n\r\nThe command-based one seems to _quietly fall back to ASCII_.\r\n\r\nAlso note that this issue won't arise on _Unix_ platforms, where PowerShell now emits UTF-8 by default and external utilities such as `grep` expect just that.\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-rc.2 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\nWindows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-12-30T02:34:56.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/5763/PowerShell/issues/5763"}
| 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:689f6db0-7988-d45a-ae79-193b7503c8d5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E624:33E6FA:3EA339:526EB2:6A5B392B |
| html-safe-nonce | 0fd9508dd6da73ce44429d30ef1a36edd5630823489590ed79bc5ba916aa2e0c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNjI0OjMzRTZGQTozRUEzMzk6NTI2RUIyOjZBNUIzOTJCIiwidmlzaXRvcl9pZCI6IjQ4Nzg4MTk2NzE2MjQzMzM2MTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c5fbe7fe96f1994eb7d4e38beea282b5a9d06eededa6fcd5e29875492b131ca4 |
| hovercard-subject-tag | issue:285179042 |
| 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/5763/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f5bf07c2af0df4c796f7a6b372be305fe3086dbc85fc05a61defb4b3fdd6ed18/PowerShell/PowerShell/issues/5763 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f5bf07c2af0df4c796f7a6b372be305fe3086dbc85fc05a61defb4b3fdd6ed18/PowerShell/PowerShell/issues/5763 |
| og:image:alt | Note: This issue may well be just a symptom of #5579; if so, it still may be of interest, because it demonstrate that the linked issue has real-world consequences. Preference variable $OutputEncodi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width