别再让 AI 写烂脚本了!这份 PowerShell 提示词配置太香了
最近在折腾本地开发环境,以前为了跟 AI 配合方便,一直用 WSL2(Windows Subsystem for Linux)。这东西虽然好用,流畅度也在线,但那个内存占用真是让人头秃,动不动就好几个 G 没了。而且,它毕竟不是 Windows 原生的文件系统,每次想右键直接打开文件夹,或者调用 Windows 原生编辑器,总会有点“隔靴搔痒”的不便。
回归原生 Windows 开发环境
痛定思痛,我最近决定回归原生 Windows 怀抱,全面拥抱 PowerShell。
但问题来了:直接甩给 AI(比如 Codex 或其他编程助手)一个“写个脚本”的指令,它生成的代码经常水土不服。要么是混用了 Bash 语法,要么是不知道加载了什么奇怪的 Profile 导致报错,要么就是文本编码全是乱码。
经过反复调试,我总结了一套超实用的提示词配置。把这个喂给 AI,生成 PowerShell 命令的准确率和运行速度直线提升,基本不怎么踩坑了。今天就分享给兄弟们,建议直接收藏。
💡 核心优化思路:让 AI 变“老实”
这套配置的核心逻辑就一句话:拒绝模糊,强制规范化。
很多时候 AI 报错,是因为它试图“聪明”地兼容各种环境,结果反而把环境搞乱了。我们给它的指令必须非常具体,比如:
- 拒绝 Profile 干扰:默认启动 PowerShell 一定要加
-NoProfile。这能加载最快的“纯净 Shell”,避免因为用户自定义了什么奇怪的函数或别名,导致 AI 生成时以为是通用命令,实际运行却报错的尴尬。 - 语法隔离:坚决禁止 Bash 和 PowerShell 语法混用。很多 AI 模型训练数据里 Linux 命令更多,一不留神就在 PowerShell 里给你甩个
ls -al或者grep出来,看着还行,一跑就废。 - 错误处理:多行脚本必须开头加上
$ErrorActionPreference = 'Stop'。这能保证脚本一旦出错直接停掉,而不是像没头苍蝇一样继续执行后面的步骤,导致修复起来无从下手。 - 编码规范:Windows 下写文件最头疼的就是编码问题。强制要求写文件时指定
-Encoding UTF8,从此告别乱码。
🚀 搜索效率的极致优化
n
除了基础语法,开发中最耗时的其实是“搜索”代码库。如果你的项目里有 node_modules 或者 .git 目录,用普通的文件搜索简直慢如蜗牛。
我在提示词里加入了一套针对 rg(Ripgrep)的强制指南,效果炸裂:
- 硬核搜索工具:强制 AI 优先使用
rg而不是 PowerShell 自带的Get-ChildItem递归搜索。这速度快了不是一点点。 - 降噪处理:明确告诉 AI,在搜索大目录(比如用户根目录)时,必须排除
.git、node_modules、虚拟环境和缓存包。这些东西全是噪声,搜出来既不看还浪费时间。 - 正则优化:复杂正则用
rg --pcre2,固定字符串用rg -F。别让 AI 生成一坨看起来很高级实则低效的模糊搜索指令。
🛠️ 开箱即用的提示词配置
废话不多说,直接把下面这段配置扔给你的 Coding Agent(比如放在 ~/.codex/AGENTS.md 或者对应的 System Prompt 区域里),效果立竿见影。
PowerShell Guidelines
- On Windows, prefer PowerShell commands and PowerShell-native cmdlets when interacting with the filesystem or environment.
- When explicitly launching PowerShell from another command, use `powershell -NoProfile` or `pwsh -NoProfile` by default to avoid user profile scripts, prompt integrations, aliases, and shell customizations. Omit `-NoProfile` only when the command intentionally depends on the user's PowerShell profile.
- Do not mix Bash syntax with PowerShell syntax in the same command or script.
- For multi-line PowerShell scripts, set `$ErrorActionPreference = 'Stop'` near the top so failures do not continue silently.
- When creating or writing text files from PowerShell, specify `-Encoding UTF8` when the cmdlet supports it.
- Prefer PowerShell object pipelines such as `Select-Object`, `Where-Object`, and `ForEach-Object` for structured data handling instead of fragile text slicing.
Search Guidelines
- For broad file or text searches, prefer `rg` / `rg --files` over recursive `Get-ChildItem`. Start from the narrowest known directory. When searching large trees such as the user home directory, exclude high-noise directories like `.git`, `node_modules`, virtualenv folders, and package caches, and use `--hidden` only when hidden directories are relevant.
- Use `rg --pcre2` for regex look-around: `(?!...)`, `(?=...)`, `(?
- In PowerShell, quote complex `rg` patterns with single quotes and escape `'` as `''`.
- Use `rg -F` for fixed strings; test complex commands before sharing.
🎯 实测感受
自从用了这套提示词,最直观的感受就是“听话”了。AI 不再动不动就抛出一堆只有在他脑子里那台 Linux 服务器上能跑的代码。现在的 Windows 原生开发体验,既保留了内存占用的优势,又打通了文件系统的便捷性,可以说是兼顾了性能体验的“甜点位”。
如果你也被 WSL2 的内存占用心态搞崩,或者受够了 AI 写脚本各种报错,不妨试一下这个方案,绝对丝滑。

评论已关闭