Johnny5
Johnny5

Reputation: 6852

Nuget powershell command - find projects with specific package installed

I want, via the nuget console, to list project that has a specific package installed.

Ex :

get-project -all | get-package | ?{ $_.Id -like 'ThePackage' } | `
    <Here I have all the packages, but I can't access the project name>

The last part should be something like Select-Object project.Name, but get-package does not return the project name, just the package information.

Upvotes: 1

Views: 2681

Answers (2)

Scott Munro
Scott Munro

Reputation: 13576

With version 2.0.0.0 of the Get-Package cmdlet, there is an entry returned for each usage of a package within a project - and the project name is included.

Check your version of Get-Package with the following command in the Package Manager Console within Visual Studio.

get-command get-package

Find all of the projects that have a specific package installed using the following command.

get-package newtonsoft.json

If you are interested in finding any packages for which projects within the solution reference different versions, then try the following command. It should display a list of packages. You can choose one, hit OK and it will show you the projects that reference them.

get-package | group-object Id | %{$versions = ($_.Group | Group-object Version); if(($versions | measure-object).Count -gt 1){$versions;};} | %{$_.Group} | group-object Id, Version | sort-object Name | Out-GridView -OutputMode Single | %{$_.Group} | select-object -first 1 | %{ $version = $_.Version; get-package $_.Id;} | ?{$_.Version -eq $version}

Upvotes: 0

x0n
x0n

Reputation: 52410

Yeah, this could be better. One way to do this is to take advantage of the fact that get-package will accept a filter on project:

Get-Project | foreach-object {
    get-package -ProjectName $_.Name | `
    Add-Member -MemberType NoteProperty -Name ProjectName -Value $_.Name -passthru
    }  | select id, projectname | ft -auto -GroupBy projectname

I'm adding the projectname property the each package before grouping on this property at the end.

Edit by Johnny5 : To get the exact result I wanted, I edited the command like this :

Get-Project -all | foreach-object { get-package -ProjectName $_.Name | `
    Add-Member -MemberType NoteProperty -Name ProjectName -Value $_.Name -passthru
} | where-object { $_.id -eq 'Id.Of.The.Package' } | select id, projectname

Upvotes: 3

Related Questions