Character Encoding and Garbled Text
When working with Japanese text in the command prompt, you may run into garbled characters. This happens often when handling UTF-8 files or processing the output of other programs.
This article explains the causes of garbled text and how to deal with it.
Why Garbled Text Happens
Garbled text occurs because the side that writes the text and the side that reads it use different character encodings.
On Japanese Windows, the following encodings coexist.
| Encoding | Code page | Description |
|---|---|---|
| Shift_JIS | 932 | Default on Japanese Windows |
| UTF-8 | 65001 | The mainstream for the web and development |
| UTF-16 | - | Used internally by Windows |
The default code page of the command prompt is 932 (Shift_JIS), but recent text editors and programs often default to UTF-8. This mismatch is the main cause of garbled text.
Checking the Current Code Page
chcp
Changing the Code Page (chcp Command)
The chcp command changes the code page.
Switch to UTF-8
chcp 65001

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Switch Back to Shift_JIS
chcp 932
A code page changed with chcp is valid only in that command prompt window. A new window returns to the default code page.
Fixing Garbled Text in Batch Files
Fix 1: Mind the Encoding When Saving the Batch File
A batch file must be saved in the same encoding as the command prompt’s code page.
| Save format | Behavior |
|---|---|
| ANSI (Shift_JIS) | Works with the default code page 932 |
| UTF-8 (no BOM) | Use together with chcp 65001 |
| UTF-8 (with BOM) | Garbage characters may appear at the beginning |
If you save a batch file as UTF-8 with a BOM, the BOM (Byte Order Mark) at the start may prevent @echo off from being recognized correctly. When saving as UTF-8, choose no BOM.
Fix 2: Change chcp Inside the Batch File
In a batch file that processes UTF-8 files, change the code page at the beginning.
@echo off
setlocal
rem Save the current code page, then switch to UTF-8
for /f "tokens=2 delims=:" %%a in ('chcp') do set ORIG_CP=%%a
chcp 65001 >nul
rem Process the UTF-8 file
type "utf8_data.txt"
rem Restore the original code page
chcp %ORIG_CP% >nul
endlocal
The wording of the chcp output varies with the display language, but the code page number always follows a colon (:), so delims=: extracts it as shown above. Saving the original code page lets you restore it reliably without hard-coding 932.
Adding >nul, as in chcp 65001 >nul, suppresses the “Active code page: 65001” message.
Running a UTF-8 batch file that contains Japanese text with chcp 65001 may cause labels and goto to be misinterpreted on older versions of Windows 10 and similar. If you distribute the batch file or are unsure of the target environment, saving as Shift_JIS (Fix 3) is the safe choice.
Fix 3: Save as Shift_JIS
When the batch file itself contains Japanese text, the most reliable method is to save it as Shift_JIS (ANSI).
In Visual Studio Code, click the encoding indicator at the bottom right, choose “Save with Encoding”, and select “Japanese (Shift JIS)”.
Making UTF-8 the System-Wide Default
On Windows 10 version 1803 and later (including Windows 11), you can make UTF-8 the default for the whole OS from the region settings.
- Open Settings → Time & Language → Language & region → Administrative language settings (running
intl.cplalso opens it) - On the “Administrative” tab, select “Change system locale”
- Check “Beta: Use Unicode UTF-8 for worldwide language support” and restart
Enabling this setting can cause older applications and batch files that assume Shift_JIS to show garbled text. Because the impact is broad, first check whether adjusting chcp or the save format solves the problem.
Common Garbled-Text Cases and Fixes
Case 1: A UTF-8 File Is Garbled with the type Command
chcp 65001 >nul
type "utf8_file.txt"
chcp 932 >nul
Case 2: findstr Cannot Search Japanese Text
findstr uses the system code page, so searching a UTF-8 file for Japanese text may not work correctly.
chcp 65001 >nul
findstr "キーワード" "utf8_file.txt"
chcp 932 >nul
With PowerShell, specifying the encoding explicitly is more reliable.
powershell -NoProfile -Command "Select-String -Path utf8_file.txt -Pattern 'キーワード' -Encoding utf8"
findstr’s UTF-8 support is incomplete, and pattern searches for Japanese text using regular expressions may not work correctly. If you need reliable UTF-8 text searching, consider PowerShell’s Select-String cmdlet.
Case 3: The Output of Another Program Is Garbled
The output of programs that default to UTF-8, such as Python or Node.js, may be garbled.
chcp 65001 >nul
python script.py
chcp 932 >nul
Alternatively, you can set the program’s output encoding with an environment variable.
set PYTHONIOENCODING=utf-8
python script.py
This makes Python emit UTF-8. It works for pipes and files, but on screen the text is still garbled if the console code page remains 932.
Case 4: A File Created by Redirection Is Garbled
In Windows PowerShell 5.1, a file saved with redirection (>) is UTF-16. Opening it in other tools may show garbled text or what looks like a space between every character.
When saving from PowerShell, specify the encoding explicitly, as in Out-File -Encoding utf8 or Set-Content -Encoding utf8. Note that -Encoding utf8 in Windows PowerShell 5.1 saves with a BOM.
Case 5: echo in a Batch File Is Garbled
This occurs when the encoding the batch file is saved in does not match the command prompt’s code page.
Match the batch file’s save format to the command prompt’s code page.
Font Settings
If some characters do not display, the command prompt’s font setting may be the cause.
- Right-click the title bar of the command prompt
- Select “Properties”
- On the “Font” tab, choose “MS Gothic” or “Consolas”
Windows Terminal (the default terminal in Windows 11 22H2 and later) displays UTF-8 well. If garbled text keeps occurring, consider using Windows Terminal.
Summary
| Problem | Fix |
|---|---|
| A UTF-8 file displays garbled | Change the code page with chcp 65001 |
| Japanese in a batch file is garbled | Save it as Shift_JIS |
| Another program’s output is garbled | chcp 65001 or a setting in the program |
| findstr cannot search Japanese | Change the code page or use PowerShell |
| UTF-8 with BOM problems | Save as UTF-8 without BOM |
Most garbled text can be fixed by making the writer’s and the reader’s encodings match.