Manage NuGet Packages with the Visual Studio Package Manager Console (original) (raw)

The Package Manager Console in Visual Studio uses PowerShell commands to interact with NuGet packages. You can use the console when there's no way to do an operation through the Package Manager UI. You can also use dotnet command-line interface (CLI) or NuGet CLI commands in the console.

This article describes how to find, install, update, and uninstall NuGet packages by using PowerShell commands in the Package Manager Console. For the complete Package Manager Console PowerShell command reference, see PowerShell reference.

Important

The PowerShell commands and arguments in this article are specific to the Visual Studio Package Manager Console. These commands differ from the PackageManagement module commands you can use in a general PowerShell environment. Each environment has commands that aren't available in the other, and commands with the same name might differ in their specific arguments.

Console availability

Starting in Visual Studio 2017, NuGet and the NuGet Package Manager install automatically when you create any .NET-related workloads in Visual Studio. You can also install the Package Manager by using the Visual Studio Installer. In the installer, select Individual components > Code tools > NuGet package manager.

You can also search for the NuGet Package Manager extension in Visual Studio under the Tools > Extensions and Updates or Extensions menu. If you're unable to use the extensions installer in Visual Studio, you can download the extension directly from https://www.nuget.org/downloads.

The Package Manager Console is built into the Package Manager for Visual Studio on Windows. Visual Studio Code doesn't include the console, but you can use the C# Dev Kit extension to manage NuGet packages. With this extension, you can run commands in the C# Dev Kit solution explorer or in the Visual Studio Code Command Palette. For more information, see NuGet in Visual Studio Code.

Quickly find and install a package

To use the Package Manager Console to quickly find and install a package, take the following steps:

  1. Open your project or solution in Visual Studio, and select Tools > NuGet Package Manager > Package Manager Console to open the Package Manager Console window.
  2. In the console, run the Find-Package command with a keyword to find the package you want to install. For example, to find packages that provide error logging modules and handlers (ELMAH), run the following command to search for the keyword elmah. If you already know the package name you want, skip this step.
Find-Package elmah  
  1. After you find the package, run the Install-Package command with the package ID to install the package. For example, to install the Elmah.MVC package, run the following command:
Install-Package Elmah.MVC  

For more details about these commands, see the Find a package and Install a package sections.

Tip

Many console operations depend on having a solution with a known path name open in Visual Studio. If you have an unsaved solution or no solution, an error message appears about not having an open or saved solution. To correct the error, create and save a solution, or save an unsaved solution.

Use console controls

To open the Package Manager Console in Visual Studio, go to the top menu, and then select Tools > NuGet Package Manager > Package Manager Console. The console is a Visual Studio window that you can arrange and position as you like. For more information, see Customize window layouts and personalize tabs.

By default, console commands operate against the specific package source and project shown in the controls at the top of the Package Manager Console window:

Screenshot of the Package Manager Console window. At the top, the Package source and Default project lists are highlighted.

Selecting a different package source or project changes the defaults for subsequent commands. To override these settings for single commands without changing the defaults, most console commands support -Source and -ProjectName options.

To manage package sources, select the gear icon, which opens the Tools > Options > NuGet Package Manager > Package Sources dialog. To clear the console's contents, select Clear Console , next to the Default project list.

Screenshot of the Package Manager Console window. Near the top, two icons are highlighted. One shows a gear. The other shows output lines and a red X.

To interrupt a long-running command, select Stop command execution , next to the Clear Console icon. For example, running Get-Package -ListAvailable -PageSize 500 lists the top 500 available packages on the default source, such as nuget.org, which can take several minutes.

Screenshot of the Package Manager Console window. Near the top, the Stop command execution icon, which looks like a red square, is highlighted.

Find a package

To find a package in the default source, use Find-Package. The following code blocks show you how to use parameters to refine your search:

Find-Package <keyword>  
Find-Package <string> -StartWith  
Find-Package <keyword> -First 100  

By default, Find-Package returns a list of 20 packages. As in the preceding example, you can use -First to specify a different number of packages.

Find-Package <package-name> -AllVersions -ExactMatch  

Install a package

To install a package into the default project, use Install-Package <package-name>. The Install-Package console command takes the following actions:

By default, Install-Package adds the package to the default project that the console window specifies. To add the package to a project that isn't the default project, use the -ProjectName option. For example, to add the Elmah.MVC package to the non-default UtilitiesLib project, run the following command:

Install-Package Elmah.MVC -ProjectName UtilitiesLib

Uninstall a package

To uninstall a package from the default project, use Uninstall-Package <package-name>. If you need to find the package name, use Get-Package to list all packages installed in the default project.

Uninstall-Package takes the following actions:

The following code blocks show you how to use the command in various scenarios:

Uninstall-Package <package-name> -RemoveDependencies  
Uninstall-Package <package-name> -Force  

Update a package

To update packages, use Update-Package. You can also use Get-Package to list available updates for installed packages. The following code blocks show you how to use parameters to modify the update scope:

Get-Package -updates  
Update-Package <package-name>  
Update-Package -ProjectName <project-name>  
Update-Package  

Use the NuGet CLI in the console

You can also do most console operations by using the NuGet CLI. However, the PowerShell console commands operate within the context of the Visual Studio saved project and solution, and often do more than their equivalent NuGet CLI commands. For example, installing a package through Install-Package adds a reference to the project file, but the NuGet CLI command doesn't. For this reason, developers who work in Visual Studio typically prefer to use the console commands rather than the NuGet CLI.

To use NuGet CLI commands in the Package Manager Console, install the NuGet.CommandLine package.

Install-Package NuGet.CommandLine

The preceding command installs the latest version of the NuGet CLI. To install a specific version, use the -Version option. For example, to install Version 4.4.1, use the following command:

Install-Package NuGet.CommandLine -Version 4.4.1

After you install the NuGet.CommandLine package, you can run all NuGet CLI commands through the Package Manager Console.

Extend the Package Manager Console

Some packages extend the Package Manager Console by adding commands. For example, the Microsoft.EntityFrameworkCore.Tools package adds commands like the following ones:

Screenshot of the Package Manager Console window. Output from the Add-Migration and Update-Database commands shows a database being created.

Set up a NuGet PowerShell profile

You can create a PowerShell profile to make your commonly used commands available in all PowerShell contexts, so you don't lose your PowerShell settings between sessions. NuGet supports a NuGet-specific profile, usually at %UserProfile%\Documents\WindowsPowerShell\NuGet_profile.ps1.

To find your user profile location, enter $profile in the console:

$profile
C:\Users\<user>\Documents\WindowsPowerShell\NuGet_profile.ps1

To determine whether a profile exists at that location, enter test-path $profile. If the command returns False, you need to create the profile with the specified name at that location. For more information, see Windows PowerShell Profiles.