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