1

I am trying to create a properties file using PowerShell 2.0.

I am using following operations:

echo SLAVE1=$MAIN_DB >result.properties
echo SLAVE2=$WB_DB >>result.properties

But the file is getting created in wrong encoding (I believe "With BOM"). Is there any way I can do my above operation and created file is in UTF-8 encoding without BOM?

Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278
IT-Sheriff
  • 154
  • 1
  • 18
  • I found the solution [here](http://superuser.com/questions/397890/convert-text-files-recursively-to-utf-8-in-powershell). Thanks for your time. – IT-Sheriff Aug 13 '16 at 09:57

1 Answers1

0

With your code, PowerShell 5.0 and Windows 10 I get UCS-2 LE BOM as encoding for the written file.

Try:

echo SLAVE1=$MAIN_DB | Out-File result.properties -Encoding utf8
echo SLAVE2=$WB_DB | Add-Content .\result.properties

The Cmdlets 'Out-File' and 'Add-Content' have both the -Encoding Parameter here you can choose between "Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode", "Default", and "OEM".

If the encoding in result is not the one that you want I think you've to use the .Net Classes like shown in Using PowerShell to write a file in UTF-8 without the BOM

Community
  • 1
  • 1
PatM0
  • 96
  • 5