360 Secure SkillHub
360 安全技能中心

powershell

该 Skill 是一套 Windows 文件写入规范:提醒在中文 Windows 环境下,PowerShell 重定向和默认编码可能把 UTF-8 中文写成乱码,要求改用 WriteFile 或 StrReplaceFile,并在必须使用 PowerShell 时显式指定 UTF-8,最后用 chcp 检查代码页。

jvy
jvy
热度 28.76
最近更新 2026-08-09 08:57:01
分类:开发依赖:读写文件 / 代码执行应用场景:代码生成 / 工作流

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add jvy/powershell
Metadata
name
powershell
description
Prevent file-writing via Windows PowerShell to avoid GBK encoding corruption. Use when Kimi Code CLI is running on Windows and the task involves creating, writing, or modifying text files, especially when content contains Chinese or other non-ASCII characters.

Avoid PowerShell GBK Encoding Issues

On Chinese Windows systems, the PowerShell console and redirection operators (>, >>) often use the system default encoding (GBK / code page 936). Writing UTF-8 text through PowerShell can silently corrupt Chinese characters into garbled text.

Prohibited practices

Do not use PowerShell to write or overwrite files:

  • echo "..." > file.txt
  • echo "..." >> file.txt
  • Set-Content file.txt "..."
  • Out-File file.txt without explicit -Encoding UTF8
  • Any other PowerShell command that creates or modifies file bytes indirectly

These methods are unreliable because encoding defaults vary by locale and PowerShell version.

Preferred approach

Always use the dedicated file tools:

  • WriteFile for creating or overwriting files
  • StrReplaceFile for editing existing files

These tools write UTF-8 deterministically and avoid shell encoding pitfalls.

When Shell is unavoidable

If a script or command must write files through PowerShell, force UTF-8 explicitly:

"content" | Out-File -FilePath "file.txt" -Encoding UTF8

Or use .NET directly:

[System.IO.File]::WriteAllText("file.txt", "content", [System.Text.Encoding]::UTF8)

Always verify the produced file is readable and not garbled before finishing.

Verify encoding

Check the active console code page:

chcp

If it returns 936, PowerShell redirection is especially unsafe for UTF-8 content. Fall back to file tools.