Update-Package (Visual Studio 中的程序包管理器控制台)Update-Package (Package Manager Console in Visual Studio)

仅在 Windows 上的 Visual Studio 中的NuGet 包管理器控制台内可用。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.

语法Syntax

Update-Package [-Id] <string> [-IgnoreDependencies] [-ProjectName <string>] [-Version <string>]
    [-Safe] [-Source <string>] [-IncludePrerelease] [-Reinstall] [-FileConflictAction]
    [-DependencyVersion] [-ToHighestPatch] [-ToHighestMinor] [-WhatIf] [<CommonParameters>]

在 NuGet 2.8 + 中,可以使用 Update-Package 来降级项目中的现有包。In NuGet 2.8+, Update-Package can be used to downgrade an existing package in your project. 例如,如果安装了 5.1.0-rc1,以下命令会将其降级到5.0.0:For example, if you have Microsoft.AspNet.MVC 5.1.0-rc1 installed, the following command would downgrade it to 5.0.0:

Update-Package Microsoft.AspNet.MVC -Version 5.0.0.

参数Parameters

参数Parameter 描述Description
IdId 要更新的包的标识符。The identifier of the package to update. 如果省略,则更新所有包。If omitted, updates all packages. -Id 开关本身是可选的。The -Id switch itself is optional.
IgnoreDependenciesIgnoreDependencies 跳过更新包的依赖项。Skips updating the package's dependencies.
ProjectNameProjectName 包含要更新的包的项目的名称,默认为 "所有项目"。The name of the project containing the packages to update, defaulting to all projects.
{2>版本<2}Version 要用于升级的版本,默认为最新版本。The version to use for the upgrade, defaulting to the latest version. 在 NuGet 3.0 + 中,版本值必须为最低、最高、HighestMinorHighestPatch (等效于-安全)之一。In NuGet 3.0+, the version value must be one of Lowest, Highest, HighestMinor, or HighestPatch (equivalent to -Safe).
SafeSafe 仅将升级限制为与当前安装的包具有相同的主版本和次版本的版本。Constrains upgrades to only versions with the same Major and Minor version as the currently installed package.
SourceSource 要搜索的包源的 URL 或文件夹路径。The URL or folder path for the package source to search. 本地文件夹路径可以是绝对路径,也可以是相对于当前文件夹的路径。Local folder paths can be absolute, or relative to the current folder. 如果省略,则 Update-Package 搜索当前选定的包源。If omitted, Update-Package searches the currently selected package source.
IncludePrereleaseIncludePrerelease 包含更新的预发布包。Includes prerelease packages for updates.
重新安装Reinstall 使用当前安装的版本 Resintalls 包。Resintalls packages using their currently installed versions. 请参阅重新安装和更新包See Reinstalling and updating packages.
FileConflictActionFileConflictAction 当要求覆盖或忽略项目引用的现有文件时要执行的操作。The action to take when asked to overwrite or ignore existing files referenced by the project. 可能的值包括Overwrite、Ignore、None、OverwriteAllIgnoreAll (3.0 +)。Possible values are Overwrite, Ignore, None, OverwriteAll, and IgnoreAll (3.0+).
DependencyVersionDependencyVersion 要使用的依赖项包的版本,可以是下列项之一:The version of the dependency packages to use, which can be one of the following:
  • 最低(默认值):最低版本Lowest (default): the lowest version
  • HighestPatch:最小主要、次要和最高修补程序的版本HighestPatch: the version with the lowest major, lowest minor, highest patch
  • HighestMinor:最小主要版本号最高的版本,最高修补程序HighestMinor: the version with the lowest major, highest minor, highest patch
  • 最高(不带参数的更新包的默认值):最高版本Highest (default for Update-Package with no parameters): the highest version
您可以使用 Nuget.Config 文件中的dependencyVersion设置设置默认值。You can set the default value using the dependencyVersion setting in the Nuget.Config file.
ToHighestPatchToHighestPatch 等效于-Safe。equivalent to -Safe.
ToHighestMinorToHighestMinor 仅将升级限制为版本与当前安装的包相同的版本。Constrains upgrades to only versions with the same Major version as the currently installed package.
WhatIfWhatIf 显示运行命令时,不实际执行更新的情况。Shows what would happen when running the command without actually performing the update.

这些参数都不接受管道输入或通配符。None of these parameters accept pipeline input or wildcard characters.

通用参数Common Parameters

Update-Package 支持以下常见的 PowerShell 参数:调试、错误操作、ErrorVariable、OutBuffer、OutVariable、PipelineVariable、Verbose、WarningAction 和 WarningVariable。Update-Package supports the following common PowerShell parameters: Debug, Error Action, ErrorVariable, OutBuffer, OutVariable, PipelineVariable, Verbose, WarningAction, and WarningVariable.

示例Examples

# Updates all packages in every project of the solution
Update-Package

# Updates every package in the MvcApplication1 project
Update-Package -ProjectName MvcApplication1

# Updates the Elmah package in every project to the latest version
Update-Package Elmah

# Updates the Elmah package to version 1.1.0 in every project showing optional -Id usage
Update-Package -Id Elmah -Version 1.1.0

# Updates the Elmah package within the MvcApplication1 project to the highest "safe" version.
# For example, if Elmah version 1.0.0 of a package is installed, and versions 1.0.1, 1.0.2,
# and 1.1 are available in the feed, the -Safe parameter updates the package to 1.0.2 instead
# of 1.1 as it would otherwise.
Update-Package Elmah -ProjectName MvcApplication1 -Safe

# Reinstall the same version of the original package, but with the latest version of dependencies
# (subject to version constraints). If this command rolls a dependency back to an earlier version,
# use Update-Package <dependency_name> to reinstall that one dependency without affecting the
# dependent package.
Update-Package ELmah –reinstall 

# Reinstall the Elmah package in just MyProject
Update-Package Elmah -ProjectName MyProject -reinstall

# Reinstall the same version of the original package without touching dependencies.
Update-Package ELmah –reinstall -ignoreDependencies