【问题标题】:Convert text file to ANSI format将文本文件转换为 ANSI 格式
【发布时间】:2017-07-25 14:20:37
【问题描述】:

我正在运行两个 Powershell 脚本。一个 Powershell 脚本将主机名添加到文本文件中。其他 Powershell 脚本将机器的 ip 地址附加到同一文件中。 因此,.txt 文件如下所示: 主机名 IP地址 但是,此文件默认以 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 

【问题讨论】:

  • 您使用什么 PowerShell 版本? [System.Text.Encoding]::Default 返回什么?你如何写入文件?

标签: powershell text unicode encoding ansi


【解决方案1】:

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

请参阅:SS64 Out-FileSet the encoding to ANSI in PowerShell 2.0

【讨论】:

    【解决方案2】:

    以下解决了我的问题:

    Out-File 'file.txt' -Append -Encoding Ascii

    这使我能够以 ANSI 格式保存文件。

    【讨论】:

      猜你喜欢
      • 2012-05-06
      • 2013-11-11
      • 1970-01-01
      • 2010-10-18
      • 2021-12-08
      • 1970-01-01
      • 2019-04-07
      • 2022-09-24
      相关资源
      最近更新 更多