You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code中切换集成PowerShell终端至v7版本

VS Code集成终端切换到PowerShell 7及脚本运行方案

问题背景

作为VS Code新手,配置工作环境时遇到如下问题:

  • 已完成PowerShell推荐设置,但集成终端始终加载PowerShell 5.1,尝试多种旧方法无效
  • 希望保留PowerShell Extension的同时,将集成终端切换为PowerShell 7
  • 当前settings.json配置片段:
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.profiles.windows": {
    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
    }
}
  • 关闭PowerShell扩展会失去编辑器内运行脚本的能力,且扩展开启时右下角显示语言版本为v5.1
  • 若无法切换终端版本,需便捷将脚本直接运行至PowerShell v7终端的方法

解决方案(基于mklement0的思路)

1. 修改终端配置强制使用PowerShell 7

默认的"source": "PowerShell"会优先调用系统注册的PowerShell 5.1,需直接指定PowerShell 7的可执行文件路径:

更新settings.json的终端配置:

"terminal.integrated.defaultProfile.windows": "PowerShell 7",
"terminal.integrated.profiles.windows": {
    "PowerShell 7": {
        "path": "pwsh.exe", // 若系统PATH未包含pwsh,需写完整路径如"C:\\Program Files\\PowerShell\\7\\pwsh.exe"
        "icon": "terminal-powershell"
    },
    // 可选保留PowerShell 5.1作为备选终端
    "Windows PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
    }
}

保存配置后重启VS Code,集成终端将默认加载PowerShell 7。

2. 同步PowerShell扩展的Shell版本

右下角显示的版本是扩展使用的Shell版本,需单独配置扩展的执行路径:
在settings.json中添加:

"powershell.powerShellExePath": "pwsh.exe"

配置后,扩展会使用PowerShell 7运行编辑器内的脚本,右下角也会显示v7版本。

3. 备选:直接运行脚本到PowerShell 7终端

若上述方法无效,可通过以下方式快速运行脚本:

  • 打开VS Code终端面板,通过下拉菜单选择已配置的PowerShell 7终端
  • 选中脚本内容,右键选择Run Selection/Line in Active Terminal(或按F8快捷键)
  • 运行整个脚本时,在PowerShell 7终端中执行:
& "C:\你的脚本路径\script.ps1"

也可创建VS Code自定义任务,绑定快捷键实现一键运行。

内容的提问来源于stack exchange,提问作者PainfulTruth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 11:36:13