Find all NuGet references in your source tree

I have a source code repository with a bunch of Visual Studio 2012 projects. I just had a need to find all NuGet references in that repo. PowerShell to the rescue! This script walks the directory tree and spits out an array of strings containing the names of all NuGet package names, with duplicates removed.

# Given the directory tree at the current location,
# search recursively for all files named "packages.config"
# and select the ID of each package. Assumes that packages.config
# is an XML document with a top-level <packages> element with a nested
# collection of <package> elements underneath.

$result = @()
ls -r packages.config | foreach {
  [xml] $data = get-content $_.fullname
  $data.packages.package | foreach {
    # Brute-force shove into an array, we'll sort it out later.
    $result += $_.id
  }
}
# It's later! Sort, and unique-ify.
$result | sort -unique

Leave a Reply

Your email address will not be published.