powershell-pwsh.mdc 1.1 KB

123456789101112131415161718192021222324252627282930
  1. ---
  2. description: Windows 终端优先使用 pwsh(PowerShell 7+),避免旧版 powershell.exe
  3. alwaysApply: true
  4. ---
  5. # PowerShell:优先 pwsh
  6. 在 **Windows** 上需要跑 PowerShell 命令或脚本时,**优先使用 `pwsh`**(PowerShell 7+ 可执行文件),不要用旧版 **`powershell.exe`**(Windows PowerShell 5.1),除非用户明确要求兼容 5.1。
  7. ## 执行方式
  8. - 直接开 PowerShell 会话或让用户在本机执行时:写 **`pwsh`**,不要写 **`powershell`**。
  9. - 单次内联执行:可用 `pwsh -NoProfile -Command "..."` 或 `pwsh -File script.ps1`。
  10. - 若环境未安装 `pwsh`,再回退到 `powershell`,并在回复里说明依赖 5.1 或建议安装 [PowerShell](https://github.com/PowerShell/PowerShell/releases)。
  11. ## 语法提示
  12. - `pwsh` 支持 `&&`、`||` 等操作符;与 5.1 行为不一致时以 **7+** 为准编写命令。
  13. - 设置工作目录优先用:`Set-Location '路径'` 或 `cd '路径'`;不要用 CMD 的 `cd /d`。
  14. ## 反例 / 正例
  15. ```powershell
  16. # ❌ 默认指向旧版
  17. powershell -Command "Get-ChildItem"
  18. # ✅ 优先
  19. pwsh -NoProfile -Command "Get-ChildItem"
  20. ```