Visual Studio Package Manager Console

I don’t know if it was from watching movies like when Hugh Jackman was pounding away on a command prompt in a high stress scene to hack into the DOD in under 60 seconds. Or maybe it stems from some deep ingrained rationalization I filed in my brain years ago that there is some long term ROI on knowing how to type commands (and abuse the tab key) over using the trusty point-and-click mouse. I’m not sure, but I do have some type of guilty pleasure with making something… anything… happen using the command prompt. I mean with all the wonderful tools out there with the shiny buttons and animated charts, I get fascinated with the simplicity of hitting the enter key after typed text to perform some virtual song and dance.

  1. Visual Studio Package Manager Console Nuget Is Not Recognized
  2. Visual Studio Package Manager Console Restore

All that package manager console is providing is access to visual studio objects, so for most packages and scenarios it just wouldn't do a thing. An uninstall package command for common scenarios (like csproj files without fancy targets) might be able to be built.

  • Updating a package simply means installing an updated version, which often restores a package to working order. In Visual Studio, the Package Manager Console provides many flexible options for updating and reinstalling packages. Updating and reinstalling packages is accomplished as follows.
  • One approach is to use the sources command: nuget.exe sources Add Update -Name feedName -UserName user -Password secret. With this you can either add that source with the credentials or update an existing one. It works perfectly.

Which takes me to today, where my command-line weapon of choice is PowerShell. If you are a Visual Studio user then you are probably familiar with installing Nuget packages using the package console manager. What you may not be as familiar with is that the package manager console is a PowerShell shell. The package manager and Nuget Ecosystem highly leverage PowerShell. There are a lot of advanced things you can do with Nuget packages when understanding PowerShell especially when you get familiar with the Visual Studio IDE API, but I’ll save talking about that for some other time. Right now I want to introduce the basics of how to take advantage of using PowerShell scripts in your development workflow.

First, let’s prove you are in a PowerShell shell. Open up a project in Visual Studio and in your package manager console type the command “ls”. You should see that the command “ls” works just like a command prompt listing your directories contents. Also, note that you are in the directory of the project. For those even less familiar with the command prompt and the “ls” command, go ahead and try America’s favorite… the “ping” command. Ah yes, it works!

Now let’s walk through the steps to run your own PowerShell script right in Visual Studio

  1. Run the command “$PROFIILE” to see the path to Visual Studio PowerShell Profile (i.e., the PowerShell Profile for the Nuget Package Manager). Open this file with your favorite text editor (TIP: You can quickly open this file by typing the command “ii $PROFILE”. I‘m liking Atom right now, so I run “atom $profile”). If you do have a profile, you can quickly create and open a new one with the command “notepad $PROFILE”.

  2. Next, you’ll notice your profile has an extension .ps1 which is a PowerShell script. Your profile is essentially a PowerShell script executed when the package manager console is initializing and where you can add your arsenal of PowerShell commands to be used during your Visual Studio experience. So now let’s add the following function to this file and then save it:

  1. After saving your profile, go back to the package manager console and run the command “. $profile” to reload your profile.

  2. Now run the command “whatIsTypical” to get the your virtual song and dance kick. Don’t feel like typing, then just type “wha” and hit the tab key to autocomplete the command.

Obviously this is tip of the iceberg material, but hopefully it is a good jumpstart for using PowerShell while you dev. I’ll leave you with an easy script I use a lot to get your imagination going. TypeScript gets used a lot in our environment and when using TypeScript you’ll occasionally get a un-configured overzealous Visual Studio project that creates .js and .js.map files all over the place from TypeScript compilations. To keep it tidy, the following script will delete all JavaScript and JavaScript map files within the current directory and subdirectories by just running the command “cleanjs”:

-->

The NuGet Package Manager Console lets you use NuGet PowerShell commands to find, install, uninstall, and update NuGet packages. Using the console is necessary in cases where the Package Manager UI does not provide a way to perform an operation. To use nuget.exe CLI commands in the console, see Using the nuget.exe CLI in the console.

Visual Studio Package Manager Console

The console is built into Visual Studio on Windows. It is not included with Visual Studio for Mac or Visual Studio Code.

Important

The commands listed here are specific to the Package Manager Console in Visual Studio, and differ from the Package Management module commands that are available in a general PowerShell environment. Specifically, each environment has commands that are not available in the other, and commands with the same name may also differ in their specific arguments. When using the Package Management Console in Visual Studio, the commands and arguments documented in this present topic apply.

Find and install a package

For example, finding and installing a package is done with three easy steps:

  1. 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. If you already know this, skip to step 3.

  3. Run the install command:

Important

All operations that are available in the console can also be done with the NuGet 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. For example, installing a package through the console adds a reference to the project whereas the CLI command does not. For this reason, developers working in Visual Studio typically prefer using the console to the CLI.

Tip

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. Open the console in Visual Studio using the Tools > NuGet Package Manager > Package Manager Console command. 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. To overrride these settings without changing the defaults, most commands support -Source and -ProjectName options.

  4. To manage package sources, select the gear icon. 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. 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

See Install-Package.

Console

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

Manager

See Uninstall-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. (You might need to rebuild the project to see it removed from the Bin folder.)
  • 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

See Get-Package and Update-Package

Find a package

See Find-Package. In Visual Studio 2013 and earlier, use Get-Package instead.

Availability of the console

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.

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. 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.

The Package Manager Console is not presently available with Visual Studio for Mac. The equivalent commands, however, are available through the NuGet CLI. Visual Studio for Mac does have a UI for managing NuGet packages. See Including a NuGet package in your project.

The Package Manager Console is not included with Visual Studio Code.

Extend the Package Manager Console

Some packages install new commands for the console. For example, MvcScaffolding creates commands like Scaffold shown below, which generates ASP.NET MVC controllers and views:

Set up a NuGet PowerShell profile

A PowerShell profile lets you make commonly-used commands available wherever you use PowerShell. NuGet supports a NuGet-specific profile typically found at the following location:

%UserProfile%DocumentsWindowsPowerShellNuGet_profile.ps1

Visual Studio Package Manager Console Nuget Is Not Recognized

To find the profile, type $profile in the console:

For more details, refer to Windows PowerShell Profiles.

Visual Studio Package Manager Console Restore

Use the nuget.exe CLI in the console

To make the nuget.exe CLI available in the Package Manager Console, install the NuGet.CommandLine package from the console: