0

I create a file in a batch script on a Windows Server 2012 r2. How can I make this a UTF-8 file?

echo MyString=Hello >> myconfig.env

It creates the file but it cannot be read by other application. If I open the file in Notepad++ it says the file is UCS-2 LE BOM.

The script is running as a part of a self-hosted Github action-runner, and I think it runs under Windows PowerShell.

I have tried to set the code page by using the following before the echo-script:

chcp 65001

and

chcp 1252

but none of these seem to make any difference.

I added Write-Output $PSVersionTable and got this:

Name Value


PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 6.3.9600.19170
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2

MaxP
  • 325
  • 2
  • 14
  • The question mentions only the command prompt interface but you've set the `powershell` tag. If there is an answer, it may heavily depend on PowerShell or not PowerShell. Please clarify your question. – harper Jun 07 '22 at 12:29
  • 1
    In addition, if the question is about PowerShell be sure to indicate what version(s) of PowerShell, because encoding support has changed quite a bit between versions. – Jeroen Mostert Jun 07 '22 at 12:31
  • If the size of the file `myconfig.env` differs from 17 bytes, what is the actual file size? – harper Jun 07 '22 at 12:31
  • @harper: The actual file is 310 characters long ( 4 lines ) – MaxP Jun 07 '22 at 12:35
  • "I think it runs under Windows PowerShell" -- does it or doesn't it? Throw in a `Write-Output $PSVersionTable` somewhere. Under `cmd` this will simply fail, under PowerShell it'll give you the version. – Jeroen Mostert Jun 07 '22 at 12:36
  • @MaxP The BOM is written before the first text characters. If there are additional lines, then the question doesn't show the relevant code. Look for the code that creates the first line. – harper Jun 07 '22 at 12:38
  • @JeroenMostert: I added the info to the question now. – MaxP Jun 07 '22 at 12:42
  • 1
    OK, I have now found that there is a better way to write the file in Powershell, using `"MyString=Hello" | Add-content -Encoding UTF8 -Path myconfig.env`. Problem solved. – MaxP Jun 07 '22 at 12:59
  • 1
    Does this answer your question? [Changing PowerShell's default output encoding to UTF-8](https://stackoverflow.com/questions/40098771/changing-powershells-default-output-encoding-to-utf-8) – phuclv Jun 07 '22 at 13:26
  • [Changing PowerShell's default output encoding to UTF-8](https://stackoverflow.com/q/40098771/995714), [Using PowerShell to write a file in UTF-8 without the BOM](https://stackoverflow.com/q/5596982/995714), [Write-Output with no BOM](https://stackoverflow.com/q/65191663/995714) – phuclv Jun 07 '22 at 13:26

1 Answers1

2

">>" is shorthand for out-file, so setting the default encoding (utf8 with bom) (at least in powershell 5.1):

$PSDefaultParameterValues=@{
  "Out-File:Encoding"={'utf8'}
}
echo MyString=Hello >> myconfig.env
Powershell 5.1:  utf8 means "utf8 with bom"  # "encoding signature"
Powershell   7:  utf8 means "utf8 no   bom"
js2010
  • 23,033
  • 6
  • 64
  • 66