powershell core 的 pwsh.exe 必须写绝对路径:windows 为 "c:program filespowershellpwsh.exe"(含空格需双引号转义),macos 为 "/usr/local/bin/pwsh",linux 为 "/opt/microsoft/powershell/7/pwsh",路径错误直接报“the system cannot find the file specified”。

PowerShell Core 的 pwsh.exe 路径怎么写才不报错
Sublime Text 不会自动找 pwsh.exe,它只认你写的绝对路径。Windows 上默认装在 C:Program FilesPowerShellpwsh.exe(PowerShell 7.x),macOS 是 /usr/local/bin/pwsh,Linux 多为 /opt/microsoft/powershell/7/pwsh。写错路径直接报 The system cannot find the file specified。
- 别用
pwsh(没后缀)或powershell(那是 Windows PowerShell 5.1) - Windows 下路径含空格必须加引号包裹,JSON 里要写成
""C:\Program Files\PowerShell\7\pwsh.exe"" - macOS/Linux 建议先终端执行
which pwsh确认真实路径,再复制进cmd数组 - 如果用的是 ARM64 版 PowerShell(如 M-series Mac 或 Windows on ARM),路径可能带
arm64后缀,比如/opt/microsoft/powershell/7-arm64/pwsh
构建系统里 -ExecutionPolicy Bypass 为什么在 macOS/Linux 也得留着
PowerShell Core 在非 Windows 平台默认策略是 RemoteSigned,但 Sublime 的构建系统仍需显式传参——不是因为策略生效,而是避免脚本里调用 Set-ExecutionPolicy 时被拒绝(尤其当脚本含模块导入或 . ./config.ps1 这类点源操作)。
-
-ExecutionPolicy Bypass在 macOS/Linux 上实际无副作用,但删掉会导致某些依赖策略检查的模块加载失败 - 不要换成
RemoteSigned:PowerShell Core 的策略解析逻辑和 Windows PowerShell 不同,Bypass最稳 - 别试图在构建系统里加
-NoLogo或-NonInteractive:前者影响调试输出,后者会让Read-Host直接抛异常
中文路径、空格、特殊字符导致 $file 展开失败怎么办
即使开了 "shell": true,$file 变量在 JSON 里仍是纯字符串,PowerShell Core 对含空格路径非常敏感。常见症状是脚本只执行了路径前半截,比如 C:My Scripts est.ps1 变成 C:My 就终止。
- 必须把
"$file"写成""$file""(注意双引号被反斜杠转义) - macOS/Linux 下如果用户目录含中文(如
/Users/张三/xxx.ps1),同样需要引号包裹,否则pwsh解析失败 -
working_dir必须设为"$file_path",否则脚本里./data.json这种相对路径会指向 Sublime 安装目录而非脚本所在目录 - 编码统一用
"encoding": "UTF-8",别用cp936或GBK——PowerShell Core 默认 UTF-8,混用会出乱码
为什么 Ctrl+B 后输出一闪就消失,且跨平台行为不一致
PowerShell Core 在 macOS/Linux 下默认不阻塞终端,执行完立即退出;Windows 上因底层 cmd/powershell.exe 行为差异,有时残留窗口。这不是 Sublime 的 bug,是 PowerShell 运行时特性。
- 临时方案:脚本末尾加
Read-Host "Press Enter to continue...",适用于交互调试 - 更可靠的做法:改用
-Command替代-File,例如"cmd": ["pwsh", "-ExecutionPolicy", "Bypass", "-Command", "& "$file"; if ($host.Name -eq 'ConsoleHost') { Read-Host }"] - 注意
Read-Host在非交互式上下文(如 CI)会卡住,生产脚本应通过环境变量控制是否暂停 - Sublime 输出面板本身不支持交互输入,所以
Read-Host在面板里无效,必须靠终端阻塞
$PSVersionTable.PSEdition 是 Core,而 Windows PowerShell 是 Desktop,所有依赖 .NET Framework 的模块(如 SqlServer)在 Core 下默认不可用——这不是构建系统能解决的,得提前确认模块兼容性。











