TechBuzzPro

How to Change File Encoding Recursively on Windows

· 6 min read · Windows & PC

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can change the encoding of many files at once on Windows by using PowerShell or a command-line tool like iconv (via Git for Windows or WSL), because Windows Explorer has no built-in batch re-encode feature. The safest approach is to run a short PowerShell script that reads each file with its current encoding and writes it back with the new one, but you must back up your files first and test on a copy. This guide walks through the reliable methods, the pitfalls, and how to verify the result.

What You'll Need

  • Windows 10 or Windows 11 (the steps use PowerShell 5.1, which is included; PowerShell 7 works too).
  • Write access to the folder tree you are converting.
  • A backup of the files, or a test copy of the folder, because re-encoding overwrites files in place.
  • Optional: Git for Windows (provides iconv and a Bash shell) or Windows Subsystem for Linux if you prefer Unix tools.
  • Optional: a text editor like Notepad++ or VS Code to check individual files before and after.

Step-by-Step

  1. Identify the current encoding. Open a few representative files in Notepad++ and check the encoding shown in the status bar (e.g., ANSI, UTF-8, UTF-8 with BOM). If files are mixed, note which subfolders use which encoding. This matters because you must tell the script what encoding to read from; guessing wrong corrupts accented characters.
  2. Make a backup. Copy the entire folder tree to another location, or create a zip archive. Re-encoding writes over the originals, and a wrong source encoding can produce garbled text that is hard to reverse.
  3. Open PowerShell in the target folder. Right-click the folder in File Explorer and choose Open in Terminal (Windows 11) or hold Shift and right-click, then choose Open PowerShell window here (Windows 10). Alternatively, press Win+X and choose Windows PowerShell, then use cd to navigate to the folder.
  4. Run a test conversion on one file. Use a small script that reads with the source encoding and writes with the target encoding. For example, to convert a single file from Windows-1252 to UTF-8:
    $content = Get-Content -Path '.\test.txt' -Encoding Default -Raw; Set-Content -Path '.\test.txt' -Value $content -Encoding UTF8
    Open the result in Notepad++ and confirm characters look correct before proceeding.
  5. Convert all files recursively. Once the test works, loop over files with Get-ChildItem -Recurse -File and apply the same read/write pattern. A typical command for Windows-1252 to UTF-8 is:
    Get-ChildItem -Recurse -File -Include *.txt,*.csv,*.html | ForEach-Object { $c = Get-Content -LiteralPath $_.FullName -Encoding Default -Raw; Set-Content -LiteralPath $_.FullName -Value $c -Encoding UTF8 }
    Adjust the -Include list to the file types you actually need. The -Encoding Default parameter tells PowerShell to read using the system's ANSI code page; if your files are already UTF-8, use -Encoding UTF8 instead. Note that -Encoding labels can differ slightly between PowerShell versions, so check the output on a test file first.
  6. Verify a sample of converted files. Open several files from different subfolders in Notepad++ and confirm the encoding indicator shows UTF-8 and that special characters (é, ü, curly quotes) render correctly. Also check that no file became empty or truncated.
  7. Handle files that fail. If some files throw errors (e.g., locked by another program), close the program and rerun the loop only on the failed files. You can log errors by adding -ErrorAction SilentlyContinue and capturing $Error, but review the list manually.
  8. Remove backups only after verification. Keep the backup until you have opened a representative set of files and confirmed the content is intact.

Common Mistakes

  • Assuming all files share one encoding. Mixed encodings in one folder tree are common; converting everything as if it were Windows-1252 will corrupt files that were already UTF-8. Check first and split the job by folder if needed.
  • Skipping the backup. Re-encoding is destructive. Without a backup, a wrong source encoding can leave you with unreadable text and no way to recover the original bytes.
  • Using the wrong PowerShell encoding name. -Encoding Default means the system ANSI code page, not UTF-8. If your files are UTF-8 without a BOM, reading them as Default will garble non-ASCII characters.
  • Forgetting the BOM question. Some tools expect a UTF-8 BOM, others do not. PowerShell's UTF8 encoding in Windows PowerShell 5.1 writes a BOM; in PowerShell 7 it does not. Test with your target application.
  • Converting binary files by accident. Always restrict the file list with -Include or -Filter; running the loop on images or executables will corrupt them.

Tips

  • Use -WhatIf on a copy of the command to preview which files would be affected before running it for real.
  • If you prefer a GUI, Notepad++ has a Convert to UTF-8 option per file, but it has no built-in recursive batch mode; third-party tools exist but verify they are from a reputable source.
  • For large trees, run the conversion in chunks by subfolder so a failure is easier to isolate.
  • Keep a log of the original encoding of each folder; it helps if you need to reverse the change.

Troubleshooting

If converted files show garbled characters (mojibake), the source encoding was wrong. Restore from backup and retry with the correct source encoding, testing on one file first. If a file is skipped, it may be read-only or locked; close any program using it and check its properties. If PowerShell reports that Get-Content cannot find the path, ensure you are in the right directory and that the path contains no unescaped wildcard characters. If the conversion appears to succeed but some files are empty, the read step likely failed silently; rerun with error output visible and check file sizes before and after. For files that must keep a BOM, specify the encoding explicitly and verify with a hex viewer or Notepad++.

Frequently Asked Questions

Can I change encoding without PowerShell?

Not natively in File Explorer. You can use Notepad++ for individual files, or install a command-line tool like iconv through Git for Windows or WSL and run it recursively from Bash.

Will this change line endings too?

No. Encoding and line endings (CRLF vs LF) are separate. PowerShell's Set-Content may normalize line endings depending on version, so check a sample if line endings matter.

How do I know which encoding my files currently use?

Open a few in Notepad++ and read the encoding in the status bar, or use PowerShell to inspect the first bytes for a BOM. There is no universal automatic detector, so sample several files.

Does converting to UTF-8 change the file content?

Only the byte representation of characters changes; the visible text should stay the same if the source encoding was identified correctly. Always verify with a sample.

Can I convert files on a network drive?

Yes, as long as you have write permission. The same commands work with UNC paths, but conversion may be slower and files can be locked by other users.

What if I only need to convert a few file types?

Restrict the loop with -Include *.txt,*.csv or similar so other files are left untouched.