6

So in my Install.ps1 I can add a reference like this:

param($installPath, $toolsPath, $package, $project)
$project.Object.References.Add("YourDLL")

How do you remove a project reference in PowerShell?

rick schott
  • 21,012
  • 5
  • 52
  • 81

2 Answers2

14

Here's what we use for Machine.Specifications:

param($installPath, $toolsPath, $package, $project)
$project.Object.References | Where-Object { $_.Name -eq 'Machine.Specifications.TDNetRunner' } | ForEach-Object { $_.Remove() }
Alexander Groß
  • 10,200
  • 1
  • 30
  • 33
8

There are some casting issues to do this in powershell.

this is the c# to remove a reference.

DTE dte = (DTE)dteObject;
        var targetProject = (VSProject)dte.GetProject(target).Object;
        var refToRemove = targetProject.References.Cast<Reference>().Where(assembly => assembly.Name.EndsWith(library, System.StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
        if (refToRemove != null)
        {
            refToRemove.Remove();
        }

If you want to use the Solution Factory nuget package you can use the powershell command that solution factory adds.

Remove-LibraryReference  projectName system.web

Here is a link the the solution factory source http://solutionfactory.codeplex.com/SourceControl/network/Forks/erichexter/PowershellRewrite

Update: new url for solution factory: https://github.com/erichexter/SolutionFactory

Eric Hexter
  • 543
  • 3
  • 8