Downgrading with Nuget

Recently I had a problem where I had blindly (and stupidly) upgraded one of my packages across an entire solution. Only to find out the version was incompatible with some of my code. Instead of making the required changes to fix the problems, due to time constraints I decided to roll back to the previous version, so I broke out the nuget package manager console.

First we need to get a list of projects that have a reference to the nuget package

 $projects =Get-Project -All | Select @{ Name="ProjectName";Expression={$_.ProjectName}}, @{Name="Has";Expression={Get-Package Newtonsoft.Json -Project $_.Name}} | where { $_.Has -ne 'Newtonsoft.Json 4.5.4' }

Then uninstall the package

$projects | select { Uninstall-Package Newtonsoft.Json -ProjectName $_.ProjectName -Force }

It is worth noting this use of the force switch here. This can be quiet dangerous as it can break other packages (see NuGet Docs)

and finally install the new one.

$projects | select { Install-Package Newtonsoft.Json -Version 4.0.8 -ProjectName $_.ProjectName }
Advertisements
This entry was posted in Development. Bookmark the permalink.

9 Responses to Downgrading with Nuget

  1. Ithilgwau says:

    Thank you for help!

  2. Jim Raden says:

    An alternative approach is something that I always do: I make NuGet upgrades their own commit in my version control system, so if something goes wrong I can rollback or otherwise revert (in Git) or reverse merge (in SVN, TFS, etc.) the commit containing the upgrade.

  3. Bruno Juchli says:

    I had to modify the first line to:
    $projects =Get-Project -All | Select @{ Name=”ProjectName”;Expression={$_.ProjectName}}, @{Name=”Has”;Expression={Get-Package NHibernate -Project $_.Name}} | where { $_.Has }

    for this to work, otherwise, line3 installed the package in all projects, not just the ones that had the new version of it.

  4. Pingback: The Will Will Web | 使用 NuGet 更新套件時將 jQuery 升級到 2.0.2 應該如何降級

  5. g says:

    Cool! Thanks 🙂

  6. Thanks, this helped a lot, but the first command did not work as is, as the Has property might be null, and it’s not a string (at least in the latest nuget version).
    To make it work correctly:

    $projects = Get-Project -All | Select @{ Name=”ProjectName”;Expression={$_.ProjectName}}, @{Name=”Has”;Expression={Get-Package Newtonsoft.Json -Project $_.Name}} | where { $_.Has -and $_.Has.ToString() -eq “Newtonsoft.Json 4.5.4” }

  7. Pingback: Log4net and EPiServer 7.1 | Anecdotes of a tech guy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s