106

我知道我可以从 PM 控制台卸载包。我遇到了另一个项目的一些依赖问题,我想重新开始,我需要一次性删除所有包。有办法吗?

4

9 回答 9

152

To get all packages from all projects in the solution use Get-Package. To get all packages from a specific project use Get-Package -ProjectName "YourProjectName".


Remove all packages from all projects in the solution

Be careful: This will uninstall ALL packages in the solution. If -Force parameter is used, packages are removed even if dependencies exist.

Get-Package | Uninstall-Package -RemoveDependencies -Force


Remove all packages from a specific project within a solution

Be careful: This will uninstall ALL packages in the project. If -Force parameter is used, packages are removed even if dependencies exist.

Get-Package -ProjectName "YourProjectName" | 
Uninstall-Package -ProjectName "YourProjectName" -RemoveDependencies -Force
于 2016-05-25T12:21:52.060 回答
86

在包管理器控制台中,只需键入:

获取包 | 卸载包-删除依赖项

于 2016-04-26T20:38:41.803 回答
33

尝试这个:

get-package | uninstall-package -removedependencies -force
于 2017-05-06T16:20:21.030 回答
15

如果要从解决方案中的每个项目中卸载每个 NuGet 包,请在 NuGet 包管理器控制台中使用它:

foreach($project in $projects){ $packages = Get-Package -ProjectName $project.Name; foreach($package in $packages){ Uninstall-Package -ProjectName $project.Name -Id $package.Id -Force} }
于 2016-06-16T11:42:48.403 回答
10

在我的例子中使用 -Force 参数会留下项目文件修改和对正常卸载包时应该删除的一些二进制文件的引用。

这是一种无需使用 -Force 参数即可从特定项目中卸载所有包的简单方法。实际上,它会一遍又一遍地尝试卸载包,直到没有包剩下,所以你会看到一些提到依赖包的错误(如果你有的话),但是随着叶包在每次迭代中被删除,它们会越来越少地出现。

另外值得一提的是,我只在 PackageManager 控制台中测试了以下 PowerShell 片段。(“工具 > NuGet 包管理器 > 包管理器控制台”)

从解决方案中的所有项目中卸载所有包

while((Get-Project -All | Get-Package).Length -gt 0) { Get-Project -All | Get-Package | Uninstall-Package }

仅删除包含单词“ WildCardSearch ”的项目

while((Get-Project -All | Where-Object ProjectName -like '*WildCardSearch*'  | Get-Package).Length -gt 0) { Get-Project -All | Where-Object ProjectName -like '*WildCardSearch*' | Get-Package | Uninstall-Package }

请注意,如果除了依赖包之外还有其他问题阻止卸载包,则此代码段将永远运行,直到您手动停止它。

于 2017-10-13T00:26:07.073 回答
4

我不相信这是可能的,所以一次卸载所有软件包。但是,正如您已经指出的那样,您可以卸载软件包,但您也可以告诉它卸载其依赖项,执行以下操作:

卸载包 OpenIdPortableArea –RemoveDependencies

这是 Marcus Hammarberg 的博客对此进行了解释:http: //www.marcusoft.net/2011/02/nuget-uninstall-remove-dependencies.html

于 2015-02-19T01:28:51.113 回答
1

更新了脚本以删除 VS 解决方案中单个项目的所有 nuget 包:

$projectName = "MyProjectName"; $packages = Get-Package -ProjectName $projectName; foreach($package in $packages){ Uninstall-Package -ProjectName $projectName -Id $package.Id -Force}
于 2019-11-13T00:04:44.413 回答
1

使用包管理器控制台自动恢复(卸载和安装而不更新到最新版本)包:

更新包-重新安装

...或仅针对特定项目:

更新包-重新安装-项目项目名称

于 2020-02-12T17:07:59.770 回答
0

虚拟老式 for 循环:

$packages = get-package
$packageId = "Apache.NMS.ActiveMQ"


$counter = 1
foreach($package in $packages){     
    if($package.Id -eq $packageId)
    {   
        Write-Host "`n$counter-Deleting Package:`n"
        $package        
        Uninstall-Package -Id $packageId -ProjectName $package.ProjectName -RemoveDependencies 
        Write-Host "`n============================================`n"
        $counter++
    }
}
于 2018-03-16T17:33:09.820 回答