NReilingh
NReilingh

Reputation: 1919

How can I reference NuGet package files from a PowerShell script?

I'm using NuGet for package management outside the context of Visual Studio. I have a packages.config file, and can nuget restore it to a packages directory in my project.

NuGet downloads each package to a directory named for the package plus its version number. I need to access files from these packages in PowerShell, but I want to avoid including the version number in the file path, since that would require a manual change if I want to advance versions in packages.config.

Upvotes: 2

Views: 1612

Answers (1)

Totem
Totem

Reputation: 1212

I would build the path myself using the packages config files

# Get solution folder
$PathToSolution = "C:\Some\Path\SolutionFolder"

# find all packages under the solution folder
$configs = gci $PathToSolution -Filter packages.config -recurse

# get content of every package config file
$configs.fullname | %{[xml]$packages = Get-Content $_;
                  $packages.packages.package | foreach{
                                               # Get pacakge attributes
                                               $packageId=$_.id;
                                               $packageVersion=$_.version;
                                               # Build the path from the build attributes
                                               $Path="$PathToSolution\packages\$packageId.$PackageVersion";
                                               Write-output $Path}}

The only thing i would add is check of uniqueness of the package path, because most likely you're using some packages in more then one project

Upvotes: 2

Related Questions