title: How do I use Powershell to add/remove references to a csproj?
body: <blockquote>
    <p>In relation to <a href="https://stackoverflow.com/questions/8955964/possible-to-remove-and-add-a-reference-to-csproj-programmatically-via-a-batch-fi">this previous question</a> I am trying to create a batch file which as part must remove and add a reference to an XML *.csproj file. I have looked at <a href="https://stackoverflow.com/questions/4882762/nuget-uninstall-ps1-remove-a-project-reference">this</a>, <a href="https://stackoverflow.com/questions/3745029/xpath-and-csproj">this</a>, <a href="https://stackoverflow.com/questions/6136947/how-do-i-remove-an-xml-node-using-powershell">this</a> and <a href="https://stackoverflow.com/questions/1719460/powershell-xpath-selection">this</a> previous question but as a powershell n00b am unable to get it working (so far). </p>
  </blockquote>
  
  <p>Can anyone help me with the following? I want to remove two specific references in a VS2010 csproj file (XML) and add a new reference. </p>
  
  <p>I opened the csproj and the reference can be found at the following location</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
  
    &lt;!--          ...         --&gt;
    &lt;!-- Omitted for brevity  --&gt;
    &lt;!--          ...         --&gt;
  
    &lt;ItemGroup Condition="'$(BuildingInsideVisualStudio)' == 'true'"&gt;
      &lt;AvailableItemName Include="Effect" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ProjectReference Include="..\SomeDirectory\SomeProjectFile.csproj"&gt;
        &lt;Project&gt;{AAB784E4-F8C6-4324-ABC0-6E9E0F73E575}&lt;/Project&gt;
        &lt;Name&gt;SomeProject&lt;/Name&gt;
      &lt;/ProjectReference&gt;
      &lt;ProjectReference Include="..\AnotherDirectory\AnotherProjectFile.csproj"&gt;
        &lt;Project&gt;{B0AA6A94-6784-4221-81F0-244A68C374C0}&lt;/Project&gt;
        &lt;Name&gt;AnotherProject&lt;/Name&gt;
      &lt;/ProjectReference&gt;
    &lt;/ItemGroup&gt;
  
    &lt;!--          ...         --&gt;
    &lt;!-- Omitted for brevity  --&gt;
    &lt;!--          ...         --&gt;
  
  &lt;/Project&gt;
  </code></pre>
  
  <p>Basically I want to:</p>
  
  <ul>
  <li>remove these two references</li>
  <li>insert a new reference to a pre-compiled DLL specified by relative path </li>
  <li>OR Add an Assembly Reference Location to the project specified by relative path </li>
  </ul>
  
  <p>As a very simple example I have tried the following powershell script to delete all the ProjectReference nodes. I pass the path to csproj as argument. I get the error <code>Cannot validate the argument 'XML'. The Argument is null or empty</code>. I can confirm it <em>is</em> loading the csproj and saving it in-place unmodified so the path is correct. </p>
  
  <pre><code>param($path)
  $MsbNS = @{msb = 'http://schemas.microsoft.com/developer/msbuild/2003'}
  
  function RemoveElement([xml]$Project, [string]$XPath, [switch]$SingleNode)
  {
      $xml | Select-Xml -XPath $XPath | ForEach-Object{$_.Node.ParentNode.RemoveAll()}
  }
  
  $proj = [xml](Get-Content $path)
  [System.Console]::WriteLine("Loaded project {0} into {1}", $path, $proj)
  
  RemoveElement $proj "//ProjectReference" -SingleNode
  
      # Also tried
      # RemoveElement $proj "/Project/ItemGroup/ProjectReference[@Include=`'..\SomeDirectory\SomeProjectFile.csproj`']" -SingleNode
      # but complains cannot find XPath
  
  $proj.Save($path)
  </code></pre>
  
  <p>What am I doing wrong? Any comments/suggestions welcome :)</p>