31 Mar

Quickly Restore NuGet Packages With PowerShell

I was recently on a project where we had many solutions to handle, lots of which had NuGet packages, and they were all checked into TFS so any new developers on the project could just get latest and get up and running. Part of our cleanup was to remove all NuGet packages from source control, which typically wouldn’t be a big deal because NuGet can automatically download missing packages. The problem really comes about because we have such an enormous library of solutions, most of which have multiple projects using NuGet, and we use an automated build script to build all projects, which was now failing because of all the missing packages. The quick solution I came up with is to create a quick PowerShell script to loop through our entire library of solutions and download all missing packages for all projects that use NuGet.

It’s a very basic script but to understand it you just need to know a tiny bit about the structure that NuGet uses. To demonstrate I just created an empty web application through visual studio with a few packages. When you add packages through NuGet you will find a .config file named packages.config in the root of the project you added it to, as well as a “packages” folder in the root of your solution where all your packages will live along with a repositories.config file which will contain the location of your packages.config file.

This is what your packages.config file will look like. Basically a list of all packages you have installed in the project, along with version and framework.

packages

 

Basic Setup: 

I have added a download to the PowerShell script at the bottom of this page. You can open it with your editor of choice and modify the $SOLUTIONROOT and $NUGETLOCATION. The SOLUTIONROOT string will be a parent folder for all your projects. We will search all child folders for any packages.config files and restore any packages listed. The NUGETLOCATION string is the path to NuGet.exe. You can get this from https://www.nuget.org/ or install it through Visual Studio.

On to the script:

Restore

 

So basically we are starting at the $SOLUTIONROOT and looking through all files for any “packages.config” and storing them in a variable called $PACKAGECONFIGS. Once we are done searching and have our list of all packages.config files, we will loop though each one and run the nuget command to restore all packages in each config file into the location $PACKAGECONFIG.Directory.parent.FullName + “\packages”- which loosely translates into “move 2 folders up (root of the solution) and into the packages folder”.

Nuget.exe install <full path to packages.config> -OutputDirectory <full path to packages folder (stored in solution root)>

And that’s pretty much it. If you have changed the name of your packages config, or the directory where your packages are stored, the script should be pretty easy to modify. So lets see it in action. I have deleted all the packages from my packages folder to simulate a fresh source control “get latest” with no packages.

packagefolder1

 

 

 

Run the script, and we have…

packagefolder2

 

 

 

 

 

 

 

 

 

 

 

 

Success!

Here is a download link to the script file.

NuGetRestore

-Joe

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>