Among the supported commands in NuGet’s Package Manager Console, Update-Package is arguably the most powerful in terms of its supported parameters and switches. The primary purpose of the command is to update package(s) in your projects to a higher (possibly latest) version. However, the provided parameters allow you to twist this behavior in many different ways. In this post, I’m going to give a quick tutorial on what possible ways you can invoke the command.
In total, the Update-Package command offers eight parameters (in addition to the built-in PowerShell parameters):
|-Id||string||Specifies the Id of the package to be updated.|
|-ProjectName||string||Specifies the name of the project in which packages should be updated.|
|-Version||SemanticVersion*||Specifies the new target version of the package as a result of the update.|
|-Source||string||Specifies where to look for package updates, overriding the package sources that are specified in the Options dialog. This value can be either a url to a remote repository or a path to a local repository or the name of a package source specified in the Options dialog.|
|-IgnoreDependencies||Switch parameter||If set, NuGet will ignore dependency packages and only update the main package.|
|-Safe||Switch parameter||If set, NuGet will only update to a new version that has the same major and minor versions as the previous package. For example, if the old version is 1.2.0, NuGet will accept the update package with version of 1.2.1 or 1.2.9999 but it will not accept 1.3.0.|
|-IncludePrerelease||Switch parameter||If set, NuGet will consider prerelease packages as candidates for updates.|
|-Reinstall||Switch parameter||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. You can't set this parameter together with the -Version parameter.|
*Although the type of the -Version parameter is SemanticVersion, which is a type declared by NuGet, you can just use string when you set it. PowerShell will automatically convert it for you, as long as the value can be parsed by the SemanticVersion class.
With that, here are the common use cases for the Update-Package command:
- Update a particular package in a project to the latest version:
Update-Package jQuery -ProjectName MyProject
- Update a particular package in a project to the latest version, using safe update rule:
Update-Package jQuery -ProjectName MyProject -Safe
- Update a particular package in a project to a particular version:
Update-Package jQuery -ProjectName MyProject -Version 1.8
- Update a particular package in all projects of the current solution to the latest version:
- Update a particular package in all projects of the current solution to a particular version:
Update-Package jQuery -version 1.8
- Reinstall a particular package in all projects of the current solution:
Update-Package jQuery -reinstall
- Update all packages in a project to the latest versions:
Update-Package -ProjectName MyProject
- Reinstall all packages in a project:
Update-Package -ProjectName MyProject -reinstall
- Update all packages in all projects of the current solution to the latest versions:
- Reinstall all packages in all projects of the current solution:
Phew, that’s a lot of different combinations; but they are quite intuitive and easy to remember. To keep the list small, I left out the -IgnoreDependencies and -IncludePrelease parameters but you can add either (or both) of them to any of the example above (except that you can’t use -Version and -Reinstall together, remember?) to adjust the behavior of the command.
Hope that helps. Happy New Year.