Install multiple packages from Visual Studio package manager console

Adding multiple nuget packages to the project from package manager console

If you are using Visual Studio 2017 as your main IDE, than you are probably familiar and used at least once NuGet package manager console. You may know or maybe not, but NuGet Package Manager relies on PowerShell script and Package Manager Console is basically hosted PowerShell command line inside Visual Studio.

This means, apart from NuGet commands, you can also execute PowerShell commands. 

Nuget Ps

This allows great extensibility as you can write any PowerShell script you need and use it from Visual Studio directly from the hosted PowerShell terminal which is Package Manager Console. 

Install-Packages PowerShell function

Now, NuGet has one limitation which makes it a bit less awesome than it is. Unlike other package managers out here for different frameworks, NuGet does not have option to install multiple packages from the single command line.

However, since it is PowerShell based, it means you can easily extend your development environment with a simple PowerShell script. The following is the script I wrote which enables me to install multiple packages. The following function wraps up Install-Package command to receive multiple packages for installation.

function Install-Packages(
    [Parameter(ValueFromRemainingArguments = $true)]$packages
) {
    Foreach ($package in $packages) {
        $packageCommand = "Install-Package " 
            
        if ($package.Split(':').Length -gt 1) {
            if ($package.Split(':')[1] -eq "pre") {
                $packageCommand += $package.Split(':')[0] + " -IncludePrerelease"
            }
            else {
                $packageCommand += $package.Split(':')[0] + " -Version " + $package.Split(':')[1]
            }
        }
        else {
            $packageCommand += $package
        }
        #echo($packageCommand)
        Invoke-Expression $packageCommand
    }
}
    

Since it is a wrapper function around Install-Package, some of the arguments used in Install-Package command are omitted and it only supports passing version and pre-release optional argument apart from mandatory package name argument. Anyway, it is a nice base if you want to extend it and suite it to your needs.

More about Package Manager PowerShell command options and argument you can find in articles Install-Package (Package Manager Console in Visual Studio) and PowerShell reference.

Making custom made PowerShell function available in Visual Studio Package Manager Console

We have the function that will install multiple packages for us, but we are not done yet. This function is not available in the PM. To do so, we need to register a profile. Full text about PowerShell profiles you can find in article Understanding the Six PowerShell Profiles. To cut the story short, you need to place the function above in one of the file paths from the table below.

Description

Path

Current User, Current Host - console

$Home\[My ]Documents\WindowsPowerShell\Profile.ps1

Current User, All Hosts 

$Home\[My ]Documents\Profile.ps1

All Users, Current Host - console 

$PsHome\Microsoft.PowerShell_profile.ps1

All Users, All Hosts 

$PsHome\Profile.ps1

Current user, Current Host - ISE

$Home\[My ]Documents\WindowsPowerShell\Microsoft.P owerShellISE_profile.ps1

 All users, Current Host - ISE 

$PsHome\Microsoft.PowerShellISE_profile.ps1

Since I am doing development on my personal laptop and there are no accounts apar from mine on the machine I created my file with the function at C:\Users\Dejan\WindowsPowerShell\Profile.ps1

If you have Visual Studio already opened, you might not have this option available in the PM right away, for the simple reason of Visual Studio 2017 reads the profile file on the startup. So restart your Visual Studio and you should have Install-Packages method available in your VS2017 PM.

The usage of the function is pretty simple since you have only pre-release and version option.

PM> Install-Packages AutoMapper:7.0.1 Microsoft.Extensions.Configuration:pre

If you need more option, you might consider extending the function to suite your requirements. 

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform includion ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

Comments for this article