Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all Projects in Solution that contain a specific NuGet Package

I have a solution that contains multiple projects. These projects contain a lot NuGet packages that I'm having to switch out with a new package - the new package has a different ID. So what I need is a Package Manager Console script to get all projects that contain a given package ID.

I've found other scripts on SO, such as Nuget powershell command - find projects with specific package installed - but that errors saying PackageName has already been added.

Any help gratefully received! Thanks

like image 808
James Woodley Avatar asked Mar 15 '23 08:03

James Woodley


1 Answers

I checked the post you referred to and played around with the script. This version works fine for me:

Get-Project -all | foreach-object {get-package -ProjectName $_.Name | `
Add-Member -Force -MemberType NoteProperty -Name ProjectName -Value $_.Name -passthru }  | `
select id, projectname | Where-Object {$_.id -eq "RestSharp"} | `
Format-Table -auto -GroupBy projectname 

I added -Force flag as the error message suggested and added filtering by package id. You need to replace the id of the package to search for a specific package (RestSharp in my example).

Also, you probably already know but it took me some time to discover so I'll mention it anyway:

You can right-click on the Solution and select "Manage NuGet Packages for Solution". Then you can click on the Manage button for an installed package. It will show a dialog box with a tree view of the solution and you can easily see which projects that package is already installed in by looking at the checkboxes on the left and easily install/uninstall that package for multiple projects.

like image 175
Volkan Paksoy Avatar answered Apr 07 '23 21:04

Volkan Paksoy