# Windows Encoding Hardening

> Fix Windows mojibake in four layers: script BOM, console output, piped stdio, shell interop.

Windows Encoding Hardening is a paid AI agent skill on Agensi by Smith John, in Testing & Debugging. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: $5 (one-time purchase)
- Creator: [Smith John](https://www.agensi.io/creators/smith-john)
- Category: [Testing & Debugging](https://www.agensi.io/skills/testing-debugging)
- Version: 1.0
- Last updated: 2026-11-19
- Security: scan passed (2026-11-19); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/windows-encoding-hardening
- Tags: coding, debugging, windows

## About this skill

Windows 上中文变乱码，99% 不是「文件坏了」，而是某一层的默认编码不是 UTF-8。

这个技能把问题分成四层，每层都给了症状、一步验证和修法：

1. **脚本文件** — .ps1 无 BOM 会被按 GBK 解码，中文字面量变乱码、中文路径报「无效的值」
2. **控制台输出** — 屏幕乱码但文件正常，先证明是显示坏不是文件坏
3. **子进程管道 stdio** — 重定向绕过 chcp 65001，中文变问号或抛编码异常
4. **shell 互操作** — Git-Bash/MSYS 改写传给 exe 的路径

另外收了三个伪装成编码问题的坑：文本模式写坏二进制、`Get-ChildItem -Recurse` 超长路径静默少算、中文路径做进程签名匹配不到导致误判进程没在跑。

文末附录带一个纯 ASCII 的取证脚本，一条命令打出 PowerShell 版本、ANSI/OEM 代码页、控制台编码、是否在 MSYS、每个文件的 BOM/UTF-8 合法性/非 ASCII 字节数、Python stdio 编码——第 1 层和第 3 层的问题一次定位。

**适合**：在 Windows 上写脚本、跑 Python、调 CLI 的人（或让 AI 代跑这些活）。
**不适合**：Linux/容器里的 locale 问题——那是另一套（LANG/locale），本技能不管。

## Requires

- Runs on Windows only
- PowerShell 5.1 or later

## Known limitations

只覆盖 Windows 上的编码问题：Linux / 容器里的 LANG、locale 不在范围内。PowerShell 7+ 默认 UTF-8，第 1 层（BOM）的规则对它用不上。技能不代跑程序，只给判层方法与取证脚本。

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the Windows Encoding Hardening skill from Agensi" and describe your task.
3. Buy it on the listing or let your agent ask you first; your agent then loads it from Agensi when it needs it. Every paid skill has a money-back guarantee: https://www.agensi.io/docs/money-back-guarantee

## Frequently asked questions

### 什么情况下真的该改代码？

只有判出是哪一层的默认值在起作用之后。先改代码是最常见的错误动作——多数情况是文件本身没问题，只是终端按 GBK 解码。

### PowerShell 7 还需要存成带 BOM 吗？

不需要。PS 7+ 默认按 UTF-8 读，第 1 层规则对它不适用；但加了 BOM 也不会坏。

### 这个技能会改我的代码或系统设置吗？

不会。它只给判定方法和一个只读的取证脚本，所有改动都由你自己（或你的 agent）执行。

### 为什么不干脆全局 chcp 65001 / 全改 UTF-8？

重定向的 stdio 会绕过 chcp；而无条件把交互控制台改成 UTF-8 会让 GBK 环境下手打中文变乱码。这是技能里明确标注的过度修正。

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
