在 Visual Studio 中使用包管理器控制台安装和管理包 (PowerShell)Install and manage packages with the Package Manager Console in Visual Studio (PowerShell)

借助 NuGet 包管理器控制台,可以使用 NuGet PowerShell 命令查找、安装、卸载和更新 NuGet 包。The NuGet Package Manager Console lets you use NuGet PowerShell commands to find, install, uninstall, and update NuGet packages. 如果包管理器 UI 未提供执行操作的方法,则必须使用控制台。Using the console is necessary in cases where the Package Manager UI does not provide a way to perform an operation. 若要在控制台中使用 nuget.exe CLI 命令,请参阅在控制台中使用 nuget.exe CLITo use nuget.exe CLI commands in the console, see Using the nuget.exe CLI in the console.

Windows 版 Visual Studio 中内置了该控制台。The console is built into Visual Studio on Windows. Visual Studio for Mac 或 Visual Studio Code 中未提供该控制台。It is not included with Visual Studio for Mac or Visual Studio Code.

查找和安装包Find and install a package

例如,通过三个简单的步骤查找和安装包:For example, finding and installing a package is done with three easy steps:

  1. 在 Visual Studio 中打开项目/解决方案,然后使用“工具”>“NuGet 包管理器”>“包管理器控制台”命令打开控制台 。Open the project/solution in Visual Studio, and open the console using the Tools > NuGet Package Manager > Package Manager Console command.

  2. 找到要安装的包。Find the package you want to install. 如果你已经知道此操作步骤,请跳至步骤 3。If you already know this, skip to step 3.

    # Find packages containing the keyword "elmah"
    Find-Package elmah
  3. 运行安装命令:Run the install command:

    # Install the Elmah package to the project named MyProject.
    Install-Package Elmah -ProjectName MyProject


控制台中可用的全部操作也可以通过 NuGet CLI 完成。All operations that are available in the console can also be done with the NuGet CLI. 但是,控制台命令在 Visual Studio 和已保存的项目/解决方案的上下文中运行,并且通常比其等效的 CLI 命令完成更多操作。However, console commands operate within the context of Visual Studio and a saved project/solution and often accomplish more than their equivalent CLI commands. 例如,通过控制台安装包会添加对项目的引用,而 CLI 命令则不会执行此操作。For example, installing a package through the console adds a reference to the project whereas the CLI command does not. 因此,在 Visual Studio 中工作的开发人员通常更愿意使用控制台而不是 CLI。For this reason, developers working in Visual Studio typically prefer using the console to the CLI.


许多控制台操作依赖于在 Visual Studio 中通过已知路径名打开解决方案。Many console operations depend on having a solution opened in Visual Studio with a known path name. 如果你有未保存的解决方案或没有解决方案,可以看到错误:“解决方案未打开或未保存。If you have an unsaved solution, or no solution, you can see the error, "Solution is not opened or not saved. 请确保你有一个已打开且已保存的解决方案。”Please ensure you have an open and saved solution." 这表示控制台无法确定解决方案文件夹。This indicates that the console cannot determine the solution folder. 保存未保存的解决方案,或者如果没有打开解决方案,则创建并保存解决方案,这些操作应该可以纠正错误。Saving an unsaved solution, or creating and saving a solution if you don't have one open, should correct the error.

打开控制台和控制台控件Opening the console and console controls

  1. 在 Visual Studio 中使用“工具”>“NuGet 包管理器”>“包管理器控制台”命令打开控制台 。Open the console in Visual Studio using the Tools > NuGet Package Manager > Package Manager Console command. 控制台是一个 Visual Studio 窗口,可以根据需要进行排列和放置(请参阅在 Visual Studio 中自定义窗口布局)。The console is a Visual Studio window that can be arranged and positioned however you like (see Customize window layouts in Visual Studio).

  2. 默认情况下,控制台命令针对窗口顶部控件中设置的特定包源和项目执行操作:By default, console commands operate against a specific package source and project as set in the control at the top of the window:


  3. 选择不同的包源和/或项目会更改后续命令的默认值。Selecting a different package source and/or project changes those defaults for subsequent commands. 要在不更改默认值的情况下覆盖这些设置,大多数命令都支持 -Source-ProjectName 选项。To overrride these settings without changing the defaults, most commands support -Source and -ProjectName options.

  4. 若要管理包源,请选择齿轮图标。To manage package sources, select the gear icon. 这是“工具”>“选项”>“NuGet 包管理器”>“包源” 对话框的快捷方式,如包管理器 UI 页中所述。This is a shortcut to the Tools > Options > NuGet Package Manager > Package Sources dialog box as described on the Package Manager UI page. 此外,项目选择器右侧的控件可清除控制台的内容:Also, the control to the right of the project selector clears the console's contents:


  5. 最右边的按钮会中断长时间运行的命令。The rightmost button interrupts a long-running command. 例如,运行 Get-Package -ListAvailable -PageSize 500 会列出默认源(例如 nuget.org)上的前 500 个包,这可能需要几分钟才能运行完毕。For example, running Get-Package -ListAvailable -PageSize 500 lists the top 500 packages on the default source (such as nuget.org), which could take several minutes to run.


安装包Install a package

# Add the Elmah package to the default project as specified in the console's project selector
Install-Package Elmah

# Add the Elmah package to a project named UtilitiesLib that is not the default
Install-Package Elmah -ProjectName UtilitiesLib

请参阅 Install-PackageSee Install-Package.

在控制台中安装包执行的步骤与安装包时会发生什么情况相同,只不过它添加了以下内容:Installing a package in the console performs the same steps as described on What happens when a package is installed, with the following additions:

  • 控制台在其窗口中显示适用的许可条款,并附带隐含协议。The Console displays applicable license terms in its window with implied agreement. 如果你不同意这些条款,应立即卸载包。If you do not agree to the terms, you should uninstall the package immediately.
  • 此外,对包的引用也会添加到项目文件中,并显示在“引用”节点下的“解决方案资源管理器”中,需要保存项目才能直接查看项目文件中的更改 。Also a reference to the package is added to the project file and appears in Solution Explorer under the References node, you need to save the project to see the changes in the project file directly.

卸载包Uninstall a package

# Uninstalls the Elmah package from the default project
Uninstall-Package Elmah

# Uninstalls the Elmah package and all its unused dependencies
Uninstall-Package Elmah -RemoveDependencies 

# Uninstalls the Elmah package even if another package depends on it
Uninstall-Package Elmah -Force

请参阅 Uninstall-PackageSee Uninstall-Package. 如果需要查找标识符,请使用 Get-Package 查看当前安装在默认项目中的所有包。Use Get-Package to see all packages currently installed in the default project if you need to find an identifier.

卸载包将执行以下操作:Uninstalling a package performs the following actions:

  • 从项目中删除对包的引用(以及正在使用的任何管理格式)。Removes references to the package from the project (and whatever management format is in use). 引用不再出现在“解决方案资源管理器”中 。References no longer appear in Solution Explorer. (可能需要重建项目才能看到它已从 Bin 文件夹中删除 。)(You might need to rebuild the project to see it removed from the Bin folder.)
  • 安装包后,撤销对 app.configweb.config 的任何更改。Reverses any changes made to app.config or web.config when the package was installed.
  • 如果没有其余包使用这些依赖项,则删除以前安装的依赖项。Removes previously-installed dependencies if no remaining packages use those dependencies.

更新包Update a package

# Checks if there are newer versions available for any installed packages
Get-Package -updates

# Updates a specific package using its identifier, in this case jQuery
Update-Package jQuery

# Update all packages in the project named MyProject (as it appears in Solution Explorer)
Update-Package -ProjectName MyProject

# Update all packages in the solution

请参阅 Get-PackageUpdate-PackageSee Get-Package and Update-Package

查找包Find a package

# Find packages containing keywords
Find-Package elmah
Find-Package logging

# List packages whose ID begins with Elmah
Find-Package Elmah -StartWith

# By default, Get-Package returns a list of 20 packages; use -First to show more
Find-Package logging -First 100

# List all versions of the package with the ID of "jquery"
Find-Package jquery -AllVersions -ExactMatch

请参阅 Find-PackageSee Find-Package. 在 Visual Studio 2013 及更早版本中,请改用 Get-PackageIn Visual Studio 2013 and earlier, use Get-Package instead.

控制台的可用性Availability of the console

从 Visual Studio 2017 开始,当你选择任何与 .NET 相关的工作负载时,会自动安装 NuGet 和 NuGet 包管理器;不过,也可以通过在 Visual Studio 安装程序中选中“单个组件”>“代码工具”>“NuGet 包管理器”选项来单独安装它 。Starting in Visual Studio 2017, NuGet and the NuGet Package Manager are automatically installed when you select any .NET-related workloads; you can also install it individually by checking the Individual components > Code tools > NuGet package manager option in the Visual Studio installer.

另外,如果你在 Visual Studio 2015 及更早版本中缺少 NuGet 包管理器,请选中“工具”>“扩展和更新...” 并搜索“NuGet 包管理器”扩展。Also, if you're missing the NuGet Package Manager in Visual Studio 2015 and earlier, check Tools > Extensions and Updates... and search for the NuGet Package Manager extension. 如果无法在 Visual Studio 中使用扩展安装程序,可以直接从 https://dist.nuget.org/index.html 下载扩展。If you're unable to use the extensions installer in Visual Studio, you can download the extension directly from https://dist.nuget.org/index.html.

Visual Studio for Mac 目前不提供包管理器控制台。The Package Manager Console is not presently available with Visual Studio for Mac. 但是,可以通过 NuGet CLI 获取等效命令。The equivalent commands, however, are available through the NuGet CLI. Visual Studio for Mac 确实有一个用于管理 NuGet 包的 UI。Visual Studio for Mac does have a UI for managing NuGet packages. 请参阅在项目中包括 NuGet 包See Including a NuGet package in your project.

Visual Studio Code 中不包含包管理器控制台。The Package Manager Console is not included with Visual Studio Code.

扩展包管理器控制台Extend the Package Manager Console

某些包为控制台安装新命令。Some packages install new commands for the console. 例如,MvcScaffolding 创建如下所示的 Scaffold 命令,用于生成 ASP.NET MVC 控制器和视图:For example, MvcScaffolding creates commands like Scaffold shown below, which generates ASP.NET MVC controllers and views:

安装和使用 MvcScaffold

设置 NuGet PowerShell 配置文件Set up a NuGet PowerShell profile

通过 PowerShell 配置文件,可以在每次使用 PowerShell 时使用常用命令。A PowerShell profile lets you make commonly-used commands available wherever you use PowerShell. NuGet 支持通常位于以下位置的 NuGet 特定配置文件:NuGet supports a NuGet-specific profile typically found at the following location:


要查找配置文件,请在控制台中键入 $profileTo find the profile, type $profile in the console:


有关更多详细信息,请参阅 Windows PowerShell 配置文件For more details, refer to Windows PowerShell Profiles.

在控制台中使用 nuget.exe CLIUse the nuget.exe CLI in the console

若要在包管理器控制台中使用 nuget.exe CLI,请从控制台安装 NuGet.CommandLine 包:To make the nuget.exe CLI available in the Package Manager Console, install the NuGet.CommandLine package from the console:

# Other versions are available, see https://www.nuget.org/packages/NuGet.CommandLine/
Install-Package NuGet.CommandLine -Version 4.4.1