Using PowerShell to write a file in UTF-8 without the BOM

前端 未结 13 1343
名媛妹妹
名媛妹妹 2020-11-22 06:25

Out-File seems to force the BOM when using UTF-8:

$MyFile = Get-Content $MyPath
$MyFile | Out-File -Encoding \"UTF8\" $MyPath

13条回答
  •  自闭症患者
    2020-11-22 07:21

    One technique I utilize is to redirect output to an ASCII file using the Out-File cmdlet.

    For example, I often run SQL scripts that create another SQL script to execute in Oracle. With simple redirection (">"), the output will be in UTF-16 which is not recognized by SQLPlus. To work around this:

    sqlplus -s / as sysdba "@create_sql_script.sql" |
    Out-File -FilePath new_script.sql -Encoding ASCII -Force
    

    The generated script can then be executed via another SQLPlus session without any Unicode worries:

    sqlplus / as sysdba "@new_script.sql" |
    tee new_script.log
    

提交回复
热议问题