Skip to content

Variable assigment to files ignores all encoding settings and uses ... ASCII? #2268

@Jaykul

Description

@Jaykul

On PowerShell 5.1 and 6 we're able to control the encoding of the pipe redirection operators so we can get UTF8 encoding when we redirect to file, however, using variable syntax and assignment does not work.

Steps to reproduce

Setting (or appending) the content using variable notation and = or += doesn't respect any of the encoding settings.

PS C:\Users\Jaykul> ${C:gear.txt} = "$([char]0x263C)"
PS C:\Users\Jaykul> gc .\gear.txt -Raw -Encoding Byte | % { "{0:x2} " -f $_ }
3f 0d 0a
PS C:\Users\Jaykul> ${C:gear.txt}
?

Expected behavior

It should work the same way that pipe redirection works:

PS C:\Users\Jaykul> [char]0x263C > gear.txt
PS C:\Users\Jaykul> ${C:gear.txt}
☼
PS C:\Users\Jaykul> gc .\gear.txt -Raw -Encoding Byte | % { "{0:x2} " -f $_ }
ff fe 3c 26 0d 00 0a 00

In PS 5.1 and 6, I can change the default to UTF8, and it encodes it that way:

PS C:\Users\Jaykul> $PSDefaultParameterValues["Out-File:Encoding"] = "utf8"
PS C:\Users\Jaykul> [char]0x263C > gear.txt
PS C:\Users\Jaykul> gc .\gear.txt -Raw -Encoding Byte | % { "{0:x2} " -f $_ }
ef bb bf e2 98 bc 0d 0a

Actual behavior

Neither the DefaultParameterValues for Out-File/Set-Content/Add-Content, nor the $OutputEncoding are respected ...

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
GitCommitId                    v6.0.0-alpha.9
BuildVersion                   3.0.0.0
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSVersion                      6.0.0-alpha
WSManStackVersion              3.0
CLRVersion

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions