如何从命令行安装 Visual Studio 代码扩展

How to install Visual Studio Code extensions from Command line

如何在代码实例打开时从命令提示符安装 Visual Studio 代码扩展。 我想从 Visual Studio 代码库安装扩展。

以下是我要安装的扩展数据

我的 Visual Studio 代码实例已打开。我想要做的是从命令提示符安装以下扩展。

我相信您想要的是将扩展名安装为 .vsix 文件。 Documentation 这里。复制过来供参考。

You can manually install an VS Code extension packaged in a .vsix file. Simply install using the VS Code command line providing the path to the .vsix file.

code --install-extension myExtensionFolder\myExtension.vsix

The extension will be installed under your user .vscode/extensions folder. You may provide multiple .vsix files on the command line to install multiple extensions at once.

To make it easier to automate and configure VS Code, it is possible to list, install, and uninstall extensions from the command line. When identifying an extension, provide the full name of the form publisher.extension, for example donjayamanne.python.

code --list-extensions
code --install-extension ms-vscode.cpptools
code --uninstall-extension ms-vscode.csharp

Documentation

根据 the documentation,您可以使用 --install-extension。例如:

code --install-extension ms-vscode.csharp

补充一下 Shan Khan 上面的回答,如果你想在 .bat 文件中安装扩展,你必须使用 call 关键字,否则你的脚本会在扩展安装完成后退出。此外,如果 code.exe 不在路径中并且您正在使用完整路径调用,请确保您指向 /bin 目录:

echo.
echo.
echo Installing VS Code Extensions...
call "C:\Program Files\Microsoft VS Code\bin\code" --install-extension ritwickdey.liveserver
call "C:\Program Files\Microsoft VS Code\bin\code" --install-extension ritwickdey.live-sass
call "C:\Program Files\Microsoft VS Code\bin\code" --install-extension ms-vscode.csharp
call "C:\Program Files\Microsoft VS Code\bin\code" --install-extension PKief.material-icon-theme
echo Done.
echo.
echo.

首先,找到完全限定的扩展名。为此,您可以右键单击给定的扩展程序,然后 select 'Copy Extension Id'(在扩展程序窗格中)。

由于其他答案已经说明了 .BAT/.CMD 语法,这里是使用 Powershell 脚本(当然可以从 CMD 执行)安装扩展的示例。

# A system-wide install of VSCode might be in: "C:\Program Files\Microsoft VS Code\bin\code"
param(
    [string] $pathToVsCodeExe = ($Env:USERPROFILE + '\AppData\Local\Programs\Microsoft VS Code'),
    [string[]] $extensions = @("editorconfig.editorconfig", "dbaeumer.vscode-eslint")
)


try {
    $originalLocation = Get-Location
    Set-Location $pathToVsCodeExe
    $extensions | ForEach-Object {
        Invoke-Expression -Command "Code --install-extension $_ --force"
    }    
}
catch {
    $_
}
finally {
    Set-Location $originalLocation    
}

@derekbaker783 提供的 PowerShell 脚本对我不起作用,它抛出一个与“代码”不是 cmdlet 相关的异常,所以我将分享一个对我有用的替代方案:

$vsCodeExec = ($Env:PROGRAMFILES) + "\Visual Studio Code\Bin\code.cmd"
$extensions = @(
    "ms-vscode.cpptools",               # C/C++ Language Support
    "ms-dotnettools.csharp",            # C# Language Support
    "dbankier.vscode-instant-markdown", # Markdown Language Support
    "ms-vscode.powershell",             # PowerShell Language Support
    "ms-python.python",                 # Python Language Support
    "rebornix.ruby",                    # Ruby Language Support
    "spences10.vba",                    # VBA Language Support
    "luggage66.vbscript",               # VBScript Language Support
    "gordonwalkedby.vbnet",             # VB.NET Language Support
    "dotjoshjohnson.xml",               # XML Language Support
    "abusaidm.html-snippets",           # HTML Snippets
    "ecmel.vscode-html-css",            # CSS Intellisense for HTML
    "formulahendry.code-runner",        # Code Runner
    "ms-vscode-remote.remote-wsl",      # VSCode Remote - WSL
    "vscode-icons-team.vscode-icons",   # Icons for VSCode
    "ms-vscode.vs-keybindings",         # Visual Studio Keymap for VSCode
    "abhiagr.livs"                      # Open/Launch in Visual Studio
) | SORT

$extensions | ForEach-Object {
    try {
        Invoke-Expression "& '$vsCodeExec' --install-extension $_ --force"
        Write-Host # New-Line
    } catch {
        $_
        Exit(1)
    }
}

Exit(0)

在 Microsoft 教程“[https://docs.microsoft.com/de-de/azure/azure-signalr/signalr-tutorial-authenticate-azure-functions][1]”中,它们显示以下内容:

func extensions install -p Microsoft.Azure.WebJobs.Extensions.SignalRService -v 1.0.0

但它不起作用。所以我从这里尝试了一部分:

code --install-extension Microsoft.Azure.WebJobs.Extensions.SignalRService -v 1.0.0

得到答案:

1.56.2
054a9295330880ed74ceaedda236253b4f39a335
x64

我希望它现在可以工作...