UPDATE: with the release of NuGet 1.0 RTM, this script has some breaking changes. For an updated version, see this post by Jon Galloway: Downloading a local NuGet repository with PowerShell.
NuGet is simply awesome. Despite its relative infancy, it has already established itself as the standard for .NET package management. You can easily add packages from the public feed or even from a local directory on your machine. Phil Haack already has an excellent post describing how you set up a local feed. His post does a great job of explaining how to set up the feed, but how do you get packages (that you didn't create locally) to put in there in the first place? One way is to simply install a package and then grab the *.nupkg file locally and then copy/paste it into whatever folder you've designated as your local NuGet feed. For example, if I've added the AutoMapper package, I can just grab it from here:
But what if I want to grab ALL of the packages from the local feed and store them all locally in a single directory? Certainly, this copy/paste process is way to manual of a process. Fortunately, the OData feed for NuGet tells you all you need to know.
Notice how the
$webClient = New-Object System.Net.WebClient $feed = [xml]$webClient.DownloadString("http://feed.nuget.org/ctp2/odata/v1/Packages") $destinationDirectory = "C:developmentLocalNuGetTest" $records = $feed | select -ExpandProperty feed | select -ExpandProperty entry | select -ExpandProperty content for ($i=0; $i -lt $records.Length; $i++) { $url = $records[$i].src $startOfQuery = $url.IndexOf("?p=") + 3 $fileName = $url.Substring($startOfQuery, $url.Length - $startOfQuery) $fullPath = ($destinationDirectory + "" + $fileName) $webClient.DownloadFile($records[$i].src, ($destinationDirectory + "" + $fileName)) }
I specify my destination directory on line 3. Then I expand the XML elements into an object that PowerShell understands. After that it's a simple matter of string parsing and downloading the files with the WebClient.
Now that you have all the packages locally, you can work offline even when you don't have access to the public feed.