NuGet – Find Project referencing Version Y of Pkg M – Part 1 of 2

Had an issue at work where Visual Studio Solution (.sln) was constantly showing that a NuGet package can be updated, even though all of the individual projects where up-to date, and greyed out.

I did some digging and had to learn PowerShell in order to find this out, as this can be used in the NuGet Package Manager Console.

First of all you will need to run the Get-Project -All to list all of the projects in the solution.

This returns a List<System.__ComObject> (C# Equivalent)

Finding out properties of an object

To Find out what variables are available in that object, I piped this output into the following commands:

  • | Select-Object -First 1 in order to only get the first object in the list
  • | Select * in order to select all properties of that object.
  • | Write-Host command in order to output all variables

This returned a list of all the available variables in the piped System.__ComObject

E.g. `@{Name=ABC; FullName=C:\SomeDir\SomeSln\ABC.csprod; …..}

Find installed packages using Get-Package command

NuGet has a command called Get-Package which lets you list all installed packages inside a Solution/Project.

It has 2 parameters which will help us:

  • -Filter M Which allows us to filter according to some string. E.g. Get-Package -Filter M to get all the packages with M in its name.
  • -First x which allows us to get the top x number of packages. E.g. Get-Package -First 1
  • -Project ABC Which allows us to filter for installed packages only in the specified project ABC

First I run Get-Package -Filter M to get a list of all the M packages installed.

This returned the following values

Id   Version
---  ------

As I am interested only in the older version. I knew I could use -First 1 in order to just get the first value.

Therefore Get-Package -Filter M -First 1 returned only the older package M(

Looping through each project

Powershell has a ForEach{} statement which you can use to loop over a collection, such as the List of System.__ComObjects returned from Get-Project -All

E.g. Get-Project | ForEach{ WriteHost }

This will loop over each returned object from Get-Project and do a Console.WriteLine(object). This basically just prints out a System.__ComObject\r\n per project.

There is a shorthand for writing ForEach{} statements in PowerShell which is %{}. Making the following 2 statements identical

Get-Project | ForEach{ Write Host }
Get-Project | %{ Write Host }    

I will use the shorthand %{} ForEach notation from now on.

Accessing the Piped Object

You can access the current piped variable using the $_ notation

Therefore you can access all the properties of this object inside the loop by $_.Property

E.g. Get-Package | %{ $_.Name } will return a list of all Project Names.(As Name is a property on the returned object from Get-Package

To Be Continued

In the next part I will talk about how using all of these, you can find out which project is referencing an older version of a NuGet package.

This should be available by 6th March 2016. Keep tuned in!