claude-code - 💡(How to fix) Fix Write tool on Windows produces UTF-8 without BOM for .ps1 files, causing PowerShell 5.1 UnexpectedToken errors

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

This bug has caused the same failure across 11 separate sessions in my project, each requiring a correction loop that wastes ~500 tokens per occurrence (~5,500 tokens total). The AI has to be re-instructed every session because the Write tool always produces files without BOM and the error only appears at runtime.

Root Cause

This bug has caused the same failure across 11 separate sessions in my project, each requiring a correction loop that wastes ~500 tokens per occurrence (~5,500 tokens total). The AI has to be re-instructed every session because the Write tool always produces files without BOM and the error only appears at runtime.

Fix Action

Fix / Workaround

Workaround (currently required)

Code Example

node -e "const fs = require('fs'); fs.writeFileSync('script.ps1', content, 'utf8');"

---

$c = [System.IO.File]::ReadAllText('script.ps1', [System.Text.Encoding]::UTF8)
[System.IO.File]::WriteAllText('script.ps1', $c, (New-Object System.Text.UTF8Encoding($true)))
RAW_BUFFERClick to expand / collapse

Bug Description

The Write tool in Claude Code writes files as UTF-8 without BOM (Byte Order Mark). On Windows, PowerShell 5.1 interprets files without a BOM as ANSI (Windows-1252). When a .ps1 script contains any non-ASCII characters (emojis, em dash , arrow , or umlauts like ä, ö, ü), PowerShell throws UnexpectedToken or ParserError at those positions.

Reproduction Steps

  1. Use the Claude Code Write tool to create a .ps1 file containing any Unicode character (e.g., emoji , em dash , arrow , or umlaut)
  2. Run the script with powershell -NoProfile -File script.ps1 on Windows
  3. Result: ParserError: UnexpectedToken at the line containing the non-ASCII character

Expected Behavior

.ps1 files written by the Write tool should include a UTF-8 BOM (EF BB BF) so PowerShell 5.1 correctly interprets them as UTF-8.

Actual Behavior

Files are written as UTF-8 without BOM → PowerShell 5.1 reads them as ANSI → Non-ASCII characters cause UnexpectedToken / ParserError.

Environment

  • OS: Windows 11 Pro (10.0.22631)
  • PowerShell: 5.1 (Windows PowerShell — NOT PowerShell 7/Core)
  • Claude Code: CLI version (latest)

Impact

This bug has caused the same failure across 11 separate sessions in my project, each requiring a correction loop that wastes ~500 tokens per occurrence (~5,500 tokens total). The AI has to be re-instructed every session because the Write tool always produces files without BOM and the error only appears at runtime.

Workaround (currently required)

Instead of Write tool for .ps1 files with Unicode content, use node.js:

node -e "const fs = require('fs'); fs.writeFileSync('script.ps1', content, 'utf8');"

Or add BOM manually after writing:

$c = [System.IO.File]::ReadAllText('script.ps1', [System.Text.Encoding]::UTF8)
[System.IO.File]::WriteAllText('script.ps1', $c, (New-Object System.Text.UTF8Encoding($true)))

Suggested Fix

When the Write tool writes a .ps1 file on Windows, prepend the UTF-8 BOM (\xEF\xBB\xBF) to the file content. Alternatively, detect the file extension and encoding context to apply BOM for file types that require it on Windows.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Write tool on Windows produces UTF-8 without BOM for .ps1 files, causing PowerShell 5.1 UnexpectedToken errors