[Windows] Changes encoding of all files in a folder

If we encounter files which were encoded with encoding not common such as Shift-JIS (common in Japanese), text might be broken when open by editor or IDE. Here is a simple solution, run below snippet on Window Power Shell to change all files in folder (even files in sub folder) to a specific encoding.

cd /path/to/a/folder
Get-ChildItem *.txt -Recurse | ForEach-Object {
$content = $_ | Get-Content
Set-Content -PassThru $_.Fullname $content -Encoding UTF8 -Force}

However, there is limitation, it can only be encoded to several supported encodings listed below:

  • ASCII Uses the encoding for the ASCII (7-bit) character set.
  • BigEndianUnicode Encodes in UTF-16 format using the big-endian byte order.
  • BigEndianUTF32 Encodes in UTF-32 format using the big-endian byte order.
  • Default Encodes using the default value: ASCII.
  • Byte Encodes a set of characters into a sequence of bytes.
  • String Uses the encoding type for a string.
  • Unicode Encodes in UTF-16 format using the little-endian byte order.
  • UTF7 Encodes in UTF-7 format.
  • UTF8 Encodes in UTF-8 format.
  • Unknown The encoding type is unknown or invalid; the data can be treated as binary.

More details can be viewed here.

Source: stackoverflow.com