在 Visual Studio Code 中指定 Powershell 7 的新方法?

Lar*_*Cai 10 powershell visual-studio-code

我正在尝试在 Windows 11 上将默认的 Powershell 5 替换为较新的 Powershell 7。

互联网上 99% 的解决方案都说要将其添加到settings.json.

"terminal.integrated.shell.windows": "C:\\Program Files\\PowerShell\\7\\pwsh.exe"
Run Code Online (Sandbox Code Playgroud)

但是,现在会显示一条红色波浪线,并显示以下消息:

这已被弃用,配置默认 shell 的新推荐方法是在 中创建终端配置文件 #terminal.integrated.profiles.windows#并将其配置文件名称设置为 中的默认值#terminal.integrated.defaultProfile.windows#。目前,这将优先于新的配置文件设置,但将来会发生变化。(2)

有一个可能相关的线程,但它只处理将其默认为本地线程Command Prompt,而不是重新调整事物Powershell 7

那么,向 VS Code 提供Powershell 7路径并将其设置为默认终端的正确新方法是什么?

mkl*_*nt0 22

前言

  • 下面的解决方案解决了所提出的问题,涉及settings.json文件的修改。

  • 然而,现在有一个GUI方法可以让您更方便地选择默认配置文件,假设您的PowerShell 7+安装位于 Visual Studio Code 已知的标准位置 - 请参阅Lee Grissom 的回答
    但是,对于 Visual Studio Code 无法自动发现的安装或自定义现有配置文件的启动选项,您仍然需要该settings.json方法。

  • 这两种方法都涉及使用 PowerShell 7 作为Visual Studio Code 集成终端中的默认通用shell。

    • 相比之下,如果您安装了PowerShell 扩展(用于编写和调试 PowerShell 代码)并且想要控制其专用 shell(PIC(PowerShell 集成控制台))中使用的版本/版本,请参阅此答案

早期的VSCode (Visual Studio Code) 版本中,"terminal.integrated.shell.*""terminal.integrated.shellArgs.*"设置确定集成终端的默认 shell 及其启动参数。

这些已被shell配置文件取代,通过属性和包含默认使用的配置文件名称"terminal.integrated.profiles.*"的关联属性定义,如下所示。"terminal.integrated.defaultProfile.*"

// Excerpt from settings.json
"terminal.integrated.profiles.windows": {
    "PowerShell_7": {
      "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
      "icon": "terminal-powershell"
    },  // ...
}

// Make the profile defined above the default profile.
"terminal.integrated.defaultProfile.windows": "PowerShell_7" 
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 打开settings.json文件进行编辑,如下所示: 从命令选项板(选择菜单命令View>Command Palette...或按Ctrl+Shift+P),选择命令Preferences: Open User Settings (JSON);搜索user json应该足以找到它。

  • 我希望 Visual Studio Code自动使用您的 v7 版本,因为它(如果已安装)通常优先于 Windows PowerShell。

  • 只需安装 powershell 扩展更新 vscode 即可使用 pwsh 7.3.3 而不是 powershell 5.1!谢谢! (2认同)