Downloading a local NuGet repository with PowerShell - Jon Galloway

Downloading a local NuGet repository with PowerShell

Update: This is now available as a NuGet package - NuGet.Downloader.  

Background

NuGet is a great way to add packages - libraries, content, and other dependencies - to your applications. It shipped with ASP.NET MVC 3 and WebMatrix, but you can use it with any .NET application type.

One of the best features of NuGet is that it runs on a feed, so package owners can publish new updates at any time. However, that can be a challenge, for a few reasons:

  1. If you want to develop but don't have access to the feed - e.g. working offline, or with spotty internet - you can't install or update your packages
  2. If you're doing a presentation and for whatever reason don't have access to the NuGet feed, your presentation flops
  3. If you're doing a presentation, a new release of a package can break your entire demo
  4. Development shops with build processes and controlled software package version migrations may want to have their developers pull from a local feed of approved packages

Last November, I headed out to India for a week of Web Camps. The first three motivations listed above came into play, and I was very happy to find that a post I'd recently read by Steve Michelotti titled Create a FULL Local NuGet Repository with PowerShell did just what I wanted - downloaded a local copy of all the NuGet packages to a folder. That's great, because NuGet lets you use a folder path as a repository, so having a local copy of all the NuGet packages means I can switch between the live feed and my local cache as desired.

A few things have changed since then:

  1. The NuGet feed structure has gone through a few changes which break Steve's script. One of the biggest changes is that the feed is broken into pages, so it currently gives you only the first 100 packages per request.
  2. The NuGet feed has grown quite a bit - a good thing, but enough so that I don't want to blindly pull the whole feed on a whim anymore.

An Updated NuGet Getter Script

I've been testing out a script since mid-December which has a bunch of additional features:

  • It follows redirects, so it should work regardless of where the feed moves
  • It handles paging over the OData feed
  • It's got an option to only get the latest release of each package - this is getting to be a bigger deal as packages continue to release new versions
  • It's got an option to skip downloads if you've already got a package
  • It's got an option to only get the "top x packages" by download count - again, a useful feature as the feed grows
  • It displays download progress, using a fancy progress bar if the environment supports it

How To Use

The easiest way to use the following script (included at the end of this post) is to open it in the PowerShell ISE (Integrated Scripting Environment). It's installed with PowerShell - did you know you had a PowerShell editor/debugger? If you're running Windows 7 (like the smart developer I know you are), just tap the Windows key and type "powershell" - you should see "Windows PowerShell ISE" show up in your Programs list.

Paste in the code, save it if you'd like, and hit F5 to run it.

2011-02-02 16h21_46

When you're done, you'll see the packages saved in your local folder. The default folder is in your profile's documents / LocalNuGet.

LocalNuGet - Folder

You can also just paste this into the  Package Manager Console, but you won't get any visual feedback.

LocalNuGet - PM Console

Using the Local Repository

Here's how to add that directory as one of the repositories NuGet can pull from. First, bring up the Package Manager Settings by clicking the icon to the right of the Package Source dropdown.

NuGet Package Manager Settings

Then just give your package source a name and fill in the folder path as the source, as shown below.

Local NuGet - Adding a package source

Now you can toggle the NuGet Package Source dropdown to pull from your Local repository whenever you want, or you can specify the source (using the full path) in the Get-Package command using the -Source parameter, e.g.

PM> Get-Package -Source "c:\users\jon\Documents\LocalNuGet"

You can read more on the Get-Package parameters in the NuGet documentation.

Next Steps

I've been thinking about turning this into a NuGet package, so I could just grab the package, run a command, and have a local package source. Thoughts?

The Script

Published Wednesday, February 2, 2011 5:01 PM by Jon Galloway
Filed under: ,

Comments

# re: Downloading a local NuGet repository with PowerShell

Yeah, package that up!

Tuesday, February 8, 2011 11:06 AM by haacked

# re: Downloading a local NuGet repository with PowerShell

I'm using the latest NuGet 1.1 with a local repo as per your blog post but I keep getting the following error when I issue a "Get-Package -ListAvailable" command:

Get-Package : File contains corrupted data.

At line:1 char:12

+ Get-Package <<<<  -ListAvailable

   + CategoryInfo          : NotSpecified: (:) [Get-Package], FileFormatException

   + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.Cmdlets.GetPackageCmdlet

I've tested with just 1 package in my local repo and it also opens ok in WinZip so I don't think it's the package that's causing this issue.

Any ideas on how I can overcome it?

Thanks.

Wednesday, February 16, 2011 9:13 PM by LyphTEC

# re: Downloading a local NuGet repository with PowerShell

NuGetify this NOW :-)

Saturday, March 5, 2011 4:18 AM by Vishal R. Joshi

# re: Downloading a local NuGet repository with PowerShell

I am turning this into a package.

Saturday, March 5, 2011 7:05 AM by Eric Hexte

# re: Downloading a local NuGet repository with PowerShell

This would be great for the Orchard.net Gallery.  Has this been been ported?

Thursday, March 17, 2011 12:58 PM by danieleli

# re: Downloading a local NuGet repository with PowerShell

Enjoyed every bit of your blog.Really looking forward to read more. Will read on...

Friday, November 2, 2012 5:23 PM by Diablo 3 Gold

# re: Downloading a local NuGet repository with PowerShell

Major thankies for the post.Much thanks again. Cool.

Friday, November 2, 2012 8:58 PM by London

# re: Downloading a local NuGet repository with PowerShell

Thanks for sharing, this is a fantastic blog.Really looking forward to read more. Great.

Thursday, November 8, 2012 6:17 AM by http://www.squidoo.com/the-tao-of-badass-download

# re: Downloading a local NuGet repository with PowerShell

Hey! Would you mind if I share your blog with my

twitter group? There's a lot of people that I think would really enjoy your content. Please let me know. Thanks

Thursday, May 9, 2013 4:58 AM by Soares

# re: Downloading a local NuGet repository with PowerShell

Very great post. I just stumbled upon your weblog and wanted

to mention that I've really loved surfing around your weblog posts. In any case I'll be subscribing

to your rss feed and I am hoping you write again very soon!

Monday, May 13, 2013 6:55 AM by Weir

# re: Downloading a local NuGet repository with PowerShell

XlwKXk Looking forward to reading more. Great blog article.

Tuesday, May 14, 2013 2:14 PM by best compact digital cameras