将文本文件转换为 ANSI 格式

Mis*_*toe 0 unicode powershell encoding text ansi

我正在运行两个 Powershell 脚本。一个 Powershell 脚本将主机名添加到文本文件中。其他 Powershell 脚本将机器的 ip 地址附加到同一文件中。因此,.txt 文件如下所示: hostname ipaddress 但是,该文件默认以 Unicode 格式保存。我该怎么做才能使文本文件以 ANSI 格式存储?

我使用 PowerShell v2.0。

[System.Text.Encoding]::Default 

IsSingleByte : True
BodyName : iso-8859-1 
EncodingName : Western European (Windows) 
HeaderName : Windows-1252 
WebName : Windows-1252
WindowsCodePage : 1252
IsBrowserDisplay : True
IsBrowserSave : True
IsMailNewsDisplay : True
IsMailNewsSave : True
EncoderFallback : System.Text.InternalEncoderBestFitFallback
DecoderFallback : System.Text.InternalDecoderBestFitFallback
IsReadOnly : True 
CodePage : 1252 
Run Code Online (Sandbox Code Playgroud)

Jas*_*ell 5

根据您输出文本的方式,您可能需要设置编码类型。使用Out-File -Encoding您使用的类型ASCII。这也取决于您使用的 Powershell 版本。

请参阅:SS64 输出文件并将编码设置为 PowerShell 2.0 中的 ANSI