NuGet: Update a package to a specific version in all projects via PowerShell or CMD

nuget update-package command line
uninstall nuget package command
nuget install-package
nuget package manager
nuget update dependencies
how to update all nuget packages in a solution
nuget powershell
nuget install specific version

We have a scenario, where different teams work on different parts of a larger product. From time to time, all Visual Studio Projects of all Solutions there need to get some of their referenced NuGet packages updated. As a result of that update, all projects in the whole source tree should reference the same versions of the packages.

Is there a way to update a specific NuGet package to a specific version in all projects using some automatic approach like PowerShell scripts?

Edit: Is there a way to somehow load the necessary parts of Visual Studio and NuGet to also update the assembly references correctly?

Example:

NuGetScript Update Solution.sln PackageName -version NewVersion

or

NuGetScript Update PathToProjects PackageName -version NewVersion

There are too many components to load all of them in Visual Studio and update them manually via package manager UI.

You can create a PowerShell script to search all the project files or packages.config files in your solution and then call nuget update command to update the packages.

However, there is a limitation when update packages outside of Visual Studio, I would recommend you to do this from Visual Studio:

The update command will download and extract all new packages to the packages folders. Assembly references will be updated in the project file, however this is limited to only existing references. If a new package has an added assembly it will not be added as part of the update command. New package dependencies will also not have their assembly references added. To perform a complete update use Visual Studio.

A quick tutorial on the Update-Package command, NuGet: Update a package to a specific version in all projects via PowerShell or CMD all projects using some automatic approach like PowerShell scripts? Please use following command line in Package Manager Console  Available only within the NuGet Package Manager Console in Visual Studio on Windows. Updates a package and its dependencies, or all packages in a project, to a newer version. In NuGet 2.8+, Update-Package can be used to downgrade an existing package in your project. For example, if you have Microsoft.AspNet.MVC 5.1.0-rc1 installed, the

Please use following command line in Package Manager Console window to update a specific package in one solution.

Get-Project -All | Update-Package PackageName -Version newVersion

NuGet Update-Package PowerShell Reference, The primary purpose of the command is to update package(s) in your offers eight parameters (in addition to the built-in PowerShell parameters): For example, if the old version is 1.2.0, NuGet will accept the update package with version of Update a particular package in a project to the latest version: Update all packages. Use the update command to update all packages. Updates all packages in a project (using packages.config) to their latest available versions. It is recommended to run restore before running update. nuget update Restore packages. Use the restore command, which downloads and installs any packages missing from the packages folder. For projects migrated to PackageReference, use msbuild -t:restore to restore packages instead.

The following script was found somewhere else. I have tried it with my projects. I don't have the original link

The following script does:

  1. run restore command on any solutions found within a folder
  2. Finds all packages.config files containing the package id name provided
  3. runs update command using the packages.config file and the package name as parameters

This assumes that nuget.exe is present in the folder where you will be running this from

param(
    [Parameter(Mandatory=$true)]
    [string]$packageId
)

Get-ChildItem *.sln -recurse | %{.\\nuget.exe restore $_.fullname}

Get-ChildItem packages.config -Recurse `
  | Where-Object {$_ | Select-String -Pattern $packageId} `
  | %{.\\nuget.exe update -Id $packageId $_.FullName}

Install and manage NuGet packages using the console in Visual , Reference for Update-Package PowerShell command in the NuGet Package and its dependencies, or all packages in a project, to a newer version. (default for Update-Package with no parameters): the highest version. # Updates all packages in all projects in the solution Update-Package Updating packages in a project or solution using PackageReference always updates to the latest version of the package (excluding pre-release packages). Projects that use packages.config can, if desired, limit update versions as described below in Constraining upgrade versions.

Reinstalling and Updating NuGet Packages, In these cases, uninstalling and then reinstalling the same version of the package nuget.exe CLI, nuget update command, For all packages, delete the package folder, To restore the state of the dependency, reinstall that specific package. By default, Update-Package affects all projects in a solution. The NuGet Package Manager Console lets you use NuGet PowerShell commands to find, install, uninstall, and update NuGet packages. Using the console is necessary in cases where the Package Manager UI does not provide a way to perform an operation.

NuGet CLI update command, Reference for the nuget.exe update command. Updates all packages in a project (using packages.config ) to their latest available versions. (To update an individual package, use nuget install without specifying a version number Note: update does not work with the CLI running under Mono (Mac OSX  If set, instead of updating the package to the latest version, NuGet will uninstall the package and reinstall the same version. This is useful when, for example, you’ve updated the target framework of your project, e.g. from .NET 4.0 to .NET 4.5, and you want to reference .NET 4.5-specific assemblies in the package.

Update all packages to specific Version from Specific Source · Issue , Package-Manager-Console update-package -Version x.x.x.x -Source and run the command with http://www.nuget.org/api/v2 as the source, you will get the In the same example, if I wanted to list all the packages in my project which (​update everything to specific version) through powershell piping. To update all the packages in your solution, first restore them, and then you can either use NuGet.exe to update the packages or from within Visual Studio you can update the packages from the Package Manager Console window, or finally you can use the Manage Packages dialog.

Comments
  • can you give me example about what u want to do ?
  • This is Package Manager Console command, which need to run in Package Manager Console window in Visual Studio.
  • Unless there's a way to load the Package Manager Console without loading Visual Studio and all projects, this can't be the accepted answer.
  • dont do this if you have <= 4.71 projects in your solution. It WILL update those as well and bring with it all sorts of nasty dependencies.