public
Last active

Script to upgrade all NuGet packages in solution to new version

  • Download Gist
Upgrade-Package.ps1
PowerShell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
###########################################################
#
# Script to upgrade all NuGet packages in solution to last version
#
# USAGE
# Place this file (Upgrade-Package.ps1) to your solution folder.
# From Package Manager Console execute
#
# .\Upgrade-Package.ps1 -PackageID:Castle.Core
#
# Do not hestitate to contact me at any time
# mike@chaliy.name, http://twitter.com/chaliy
#
# Update to NuGet 1.1 is done by JasonGrundy, see comments bellow
#
##########################################################
 
param($PackageID)
 
$packageManager = $host.PrivateData.packageManagerFactory.CreatePackageManager()
 
foreach ($project in Get-Project -all) {
$fileSystem = New-Object NuGet.PhysicalFileSystem($project.Properties.Item("FullPath").Value)
$repo = New-Object NuGet.PackageReferenceRepository($fileSystem, $packageManager.LocalRepository)
 
foreach ($package in $repo.GetPackages() | ? {$_.Id -eq $PackageID}) {
Update-Package $package.Id -Project:$project.Name
}
}

In NuGet 1.1 you can use:
$packageManager = $host.PrivateData.packageManagerFactory.CreatePackageManager()

The complete script for NuGet 1.1 would be:

param($PackageID)

$packageManager = $host.PrivateData.packageManagerFactory.CreatePackageManager()

foreach ($project in Get-Project) {
$fileSystem = New-Object NuGet.PhysicalFileSystem($project.Properties.Item("FullPath").Value)
$repo = New-Object NuGet.PackageReferenceRepository($fileSystem, $packageManager.LocalRepository)

foreach ($package in $repo.GetPackages() | ? {$_.Id -eq $PackageID}) {
Update-Package $package.Id -Project:$project.Name
}
}

Which would be invoked using:

Get-Package foo –Updates | Get-Unique | ForEach-Object { .\Upgrade-Package.ps1 $_.Id }

davidfowl & JasonGrundy thanks for pointing out, script has been updated with you changes.

This script will update only default project in solution. You need to specify "Get-Project -all" in foreach loop.

Hm, didn't tested last version, let me fix this.

can't seem to get this working ...

When running:
PM> Get-Package -updates

Id Version Description


DataAnnotationsExtensions 0.5.0.0 Validation attributes that can be used in any .NET 4.0 project to supplement the existing Data Annotations attributes. ...

EFCodeFirst 1.0 Legacy package, Code First is now included in the 'EntityFramework' package.

jQuery 1.5.2 jQuery is a new kind of JavaScript Library....

jQuery.vsdoc 1.5.2 Includes vsdoc files for jQuery 1.5.2 that provide IntelliSense in Visual Studio 2010. For best experience get the JScript Edi...
StructureMap-MVC3 1.0.6 Adds the latest version of structuremap and configures it as the default Dependency Resolver.

WebActivator 1.4 A NuGet package that allows other packages to execute some startup code in web apps

So there are stuff to update ...

Running:
PM> ./Upgrade-Package.ps1
PM>

Nothing happens

I need to run:
PM> ./Upgrade-Package.ps1 jQuery.vsdoc
'jQuery 1.5.2' already installed.
Successfully installed 'jQuery.vsdoc 1.5.2'.
Successfully removed 'jQuery.vsdoc 1.5.1' from TravianBox.Web.
Successfully uninstalled 'jQuery.vsdoc 1.5.1'.
Successfully removed 'jQuery 1.5.1' from TravianBox.Web.
Successfully uninstalled 'jQuery 1.5.1'.
Successfully added 'jQuery 1.5.2' to TravianBox.Web.
Successfully added 'jQuery.vsdoc 1.5.2' to TravianBox.Web.
PM>

To actually update any packages ...

Am I doing something wrong here? Shuld it not be possible to update all packages within a solution ?

mvh
Mikael Syska

Mikael,

I've never used this script to update everything. I was updating everything
on a client's local local feed (a bunch of internal libraries) so I was
executing "Upgrade-Package.ps1 foo" and that definitely worked. I have seen
some odd behavior when some of the different projects use different versions
so that could be what you are seeing. Which of the 3 versions are you using?

Jason.

On Thu, Mar 31, 2011 at 3:25 PM, syska <
reply@reply.github.com>wrote:

can't seem to get this working ...

When running:
PM> Get-Package -updates

Id Version Description


DataAnnotationsExtensions 0.5.0.0 Validation attributes
that can be used in any .NET 4.0 project to supplement the existing Data
Annotations attributes. ...
EFCodeFirst 1.0 Legacy package, Code
First is now included in the 'EntityFramework' package.
jQuery 1.5.2 jQuery is a new kind of
JavaScript Library....
jQuery.vsdoc 1.5.2 Includes vsdoc files
for jQuery 1.5.2 that provide IntelliSense in Visual Studio 2010. For best
experience get the JScript Edi...
StructureMap-MVC3 1.0.6 Adds the latest version
of structuremap and configures it as the default Dependency Resolver.
WebActivator 1.4 A NuGet package that
allows other packages to execute some startup code in web apps

So there are stuff to update ...

Running:
PM> ./Upgrade-Package.ps1
PM>

Nothing happens

I need to run:
PM> ./Upgrade-Package.ps1 jQuery.vsdoc
'jQuery 1.5.2' already installed.
Successfully installed 'jQuery.vsdoc 1.5.2'.
Successfully removed 'jQuery.vsdoc 1.5.1' from TravianBox.Web.
Successfully uninstalled 'jQuery.vsdoc 1.5.1'.
Successfully removed 'jQuery 1.5.1' from TravianBox.Web.
Successfully uninstalled 'jQuery 1.5.1'.
Successfully added 'jQuery 1.5.2' to TravianBox.Web.
Successfully added 'jQuery.vsdoc 1.5.2' to TravianBox.Web.
PM>

To actually update any packages ...

Am I doing something wrong here? Shuld it not be possible to update all
packages within a solution ?

mvh
Mikael Syska

Reply to this email directly or view it on GitHub:
https://gist.github.com/747529

I have used it with 1.1 ... and well and a few days ago: 1.2 came out of NuGet ... so I dont have any projects atm that needs updating ...

So ... I think I'm getting a little closer to a solution ... of some sort.

When there are still old versions laying around in the packages directory, even though the new version is also there, it keeps telling me that there were updates.

Removed the old version solved that issue.

So now when running:
PM> Get-Package -updates
No package updates are available from the current package source.
PM>

I love NuGet ... but this is a odd bug ... maybe because of the nature of T4MVC ?

Or the DataAnnotationsExtensions. Since it was actually the same file with version 0.4.0.0 and 0.5.0.0 ... but the DataAnnotationsExtensions.MVC3 had changed ... Still the version number of the dll's were still 0.4.0.0

I'm no expert in this area ... but could that cause problems maybe ?

This is awesome. I wrote a NuGet package based on this: http://nuget.org/List/Packages/NuGetPackageUpdater

It overrides the default update-package and makes it global.

Mikael, Upgrade-Package.ps1 supports only updating single dependency. Pls note that this script is not official or something else. I just wrote it for my purposes.

ferventcoder, thats nice. And it supports updating all packages, so Mikael can use it.

Also today Haacked posted survey about related stuff coming to next versions of nuget http://haacked.com/archive/2011/04/06/nuget-needs-your-input.aspx .

I just read the first line in the description: "Script to upgrade all NuGet packages in solution to last version" and assumed it updated all packages to there latest version I none argument was given on the line.

Yes, Now I know that for sure and I will be looking at both NuGetPackageUpdater and have allready voted on Haacked's blog.

But still thanks very much for this script since it still helped me.

mvh

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.