none
What command to write UTF-8 BOM to file? RRS feed

  • General discussion

  • As the title stated, suppose I create a new text file, and I know I'm going to dump UTF-8 into it.

    By my unexhaustive search, trial and error, I found that out-file could allow me to write string in UTF-8, like this:

    Echo $line | Out-File -Append -Encoding UTF8 $myFile

    However, UTF-8 BOM is not written in the text file.

    Anyone knows how to dump EF BB BF inside it?

    Thanks


    Friday, February 19, 2016 5:40 PM

All replies

  • The BOM is not visible  in a text editor. You have to open the file in binary mode to see it.

    I open a file created thusly:

    'hello world' | Out-File -Append -Encoding UTF8 test.utf

    In my binate editor the BO is clearly visible.


    \_(ツ)_/

    Friday, February 19, 2016 5:54 PM
  • PS C:\scripts> $f=[io.fileinfo]"$pwd\test.utf"
    PS C:\scripts> $strm=$f.OpenRead()
    PS C:\scripts> $strm.ReadByte()
    239
    PS C:\scripts> $strm.ReadByte()
    187
    PS C:\scripts> $strm.ReadByte()
    191
    PS C:\scripts> $strm.ReadByte()
    104
    PS C:\scripts> '{0:x} {1:x} {2:x} {3:x}' -f 239,187,191,104
    ef bb bf 68

    Note the first four  bytes.  68 is 'h'.


    \_(ツ)_/



    • Edited by jrv Friday, February 19, 2016 6:02 PM
    Friday, February 19, 2016 6:01 PM
  • The BOM is not visible  in a text editor. You have to open the file in binary mode to see it.

    I open a file created thusly:

    'hello world' | Out-File -Append -Encoding UTF8 test.utf

    In my binate editor the BO is clearly visible.


    \_(ツ)_/

    My wrong.  I was reusing an existing code and didn't see that add-content was used before my own code.

    This discussion is thus invalid.

    Friday, February 19, 2016 6:18 PM