49

I have a version of a package installed in my project but during testing I have found a problem with it. I tried the obvious thing Update-Package -Id Foo.Bar -Version 1.0.0 -Force but the Update-Package cmdlet doesn't have a -Force parameter, and it doesn't allow updates to an earlier version. How do I downgrade my package dependencies (without taking advantage of source control!)


NOTE: This question is now irrelevant because Update-Package MyPackage -Version [an earlier version] works out of the box in recent versions of NuGet Package Manager. You don't even need a -Force switch.

Damian Powell
  • 8,655
  • 7
  • 48
  • 58

3 Answers3

35

I think I already have a solution to this so I place it here for (constructive) criticism.

function Reinstall-Package {

    param(
        [Parameter(Mandatory = $true)]
        [string]
        $Id,

        [Parameter(Mandatory = $true)]
        [string]
        $Version,

        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [string]
        $ProjectName,

        [switch]
        $Force
    )

    if (-not $ProjectName) {
        $ProjectName = (get-project).ProjectName
    }

    Uninstall-Package -ProjectName $ProjectName -Id $Id -Force:$Force
    Install-Package -ProjectName $ProjectName -Id $Id -Version $Version

}

This allows us to use a call such as the following to update all references to a package within the current solution.

 Get-Project -All |
     ?{ $_ | Get-Package | ?{ $_.Id -eq 'Foo.Bar' } } |
         %{ $_ | Reinstall-Package -Id Foo.Bar -version 1.0.0 -Force }

The -Force switch allows the package to be reinstalled even if it has dependent packages within the project.

Damian Powell
  • 8,655
  • 7
  • 48
  • 58
  • I didn't go through the trouble of creating the function, but the function made clear how to do this from the PM console nonetheless. Thanks! – Marc L. Mar 23 '12 at 19:31
  • 8
    For those who do not know. Too add a function to package manager console add "NuGet_profile.ps1" to \WindowsPowerShell – AdamSane Sep 25 '12 at 17:48
  • 3
    Good point, @AdamSane. Another way to edit your profile that works in any PowerShell host is `echo '' >> $profile; notepad $profile`. This will create the profile if it doesn't exist (or add an empty line to the end if it does), then open the file in notepad. – Damian Powell Sep 26 '12 at 09:34
  • Could you add a bit more clarification Damian? And perhaps another shorthand function to reinstall for all projects in solution? 1st error I got, was missing the `WindowsPowerShell` directory, so your above guide did not work out of the box. The 2nd was that I misunderstood you answer and attempted to use `Reinstall-Package` for a full reinstall. The 3rd thing that puzzled me, was the amount of warnings ('`... could not be found in your workspace, or you do not have permission to access it.`') this call creates, could that perhaps be done silently? Apart from that? Awesome! :D – Johny Skovdal Mar 14 '13 at 09:11
  • 1
    You should consider adding a -Prerelease switch to the `Reinstall-Package` method here in case the version you need is the pre-release version. – Haacked Apr 19 '13 at 04:40
11

https://docs.nuget.org/consume/package-manager-console-powershell-reference

With NuGet 2.8 client or higher, Install-Package can be used to downgrade the existing packages in your project, if necessary. For example, if you had installed a pre-release version of a package to try out new features but would like to go back to a previous stable version you can do so using Install-Package (or Update-Package).

Frol
  • 141
  • 1
  • 5
  • I <3 answers that fix a problem from the future, there should be an "updated answer" green checkmark – felickz Jun 19 '15 at 20:33
5

I had Foo.Bar v1 that depended on log4net v2, I needed to downgrade the log4net dependency to 1.2.10 so i made Foo.Bar v1.1 depend on log4net v1.2.10.

I found that if you Update-Package Foo.Bar it will update to the latest version (it won't reinstall dependencies)

But then you can Update-Package -Id Foo.Bar -Reinstall and that should reinstall the whole thing with the current dependencies.

Jeff Martin
  • 10,812
  • 7
  • 48
  • 74