简述
背景
Win11 21H2(OS Build 22000.2057)
Windows PowerShell 5.1
$psversiontable
~ > $psversiontable
Name Value
---- -----
PSVersion 5.1.22000.2003
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22000.2003
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
0x03 Character Encoding
In general, Windows PowerShell uses the Unicode UTF-16LE encoding by default.
修改默认编码
$PSDefaultParameterValues
$OutputEncoding
- The automatic variable
$OutputEncoding
affects the encoding PowerShell uses to communicate with external programs. - It has no effect on the encoding that the output redirection operators and PowerShell cmdlets use to save to files.
- The automatic variable
例:
- $PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
相关 Cmdlet
Beginning in PowerShell 5.1, the redirection operators (
>
and>>
) call theOut-File
cmdlet.
具体表现:
Out-File
and the redirection operators>
and>>
create UTF-16LE.Set-Content
andAdd-Content
useDefault
encoding.New-Item -Type File -Value
creates a BOM-less UTF-8 file.Get-Content
uses theDefault
ANSI encoding, in the absence of a BOM.
BOM (byte-order-mark)
In Windows PowerShell, any Unicode encoding, except
UTF7
, always creates a BOM.PowerShell (v6 and higher) defaults to
utf8NoBOM
for all text output.