title: Powershell - Add one xml node at multiple positions
body: <p>I recently started learning powershell to change XML files. Now I’m having trouble adding new nodes at specific positions. 
  I created one xml node 
  <code>&lt;Name2&gt;Reference&lt;/Name2&gt;</code> 
  and want to insert it in every <code>&lt;VFI&gt;</code> node within one <code>&lt;customer&gt;</code> node.</p>
  
  <p>XML File: </p>
  
  <pre><code>&lt;WartbareEinheitenListe&gt;
   &lt;WartbareEinheit&gt;
    &lt;Name&gt;John Wayne&lt;/Name&gt;
    &lt;Typ&gt;STRUKTUR&lt;/Typ&gt;
    &lt;Customer&gt;100008&lt;/Customer&gt;
    &lt;WartbareEinheiten&gt;
     &lt;WartbareEinheit&gt;
      &lt;Name&gt;Wohnhaus&lt;/Name&gt;
      &lt;Typ&gt;OBJECT&lt;/Typ&gt;
      &lt;Customer&gt;100008&lt;/Customer&gt;
      &lt;VIF&gt;                                     &lt;---- Insert here
       &lt;InfoFeld&gt;
        &lt;Name&gt;Nummer&lt;/Name&gt;
        &lt;InfoFeldTyp&gt;DECIMAL&lt;/InfoFeldTyp&gt;
         &lt;Inhalt&gt;
          &lt;Nummer&gt;4500514&lt;/Nummer&gt;
         &lt;/Inhalt&gt;
       &lt;/InfoFeld&gt;
      &lt;/VIF&gt;
    &lt;/WartbareEinheit&gt;
    &lt;WartbareEinheit&gt;
     &lt;Name&gt;John Wayne&lt;/Name&gt;
     &lt;Typ&gt;OBJECT&lt;/Typ&gt;
     &lt;Customer&gt;100008&lt;/Customer&gt;
      &lt;VIF&gt;                                    &lt;---- Insert here
       &lt;InfoFeld&gt;
        &lt;Name&gt;Nummer&lt;/Name&gt;
        &lt;InfoFeldTyp&gt;DECIMAL&lt;/InfoFeldTyp&gt;
        &lt;Inhalt&gt;
         &lt;Nummer&gt;5002449&lt;/Nummer&gt;
        &lt;/Inhalt&gt;
       &lt;/InfoFeld&gt;
      &lt;/VIF&gt;
     &lt;/WartbareEinheit&gt;
    &lt;/WartbareEinheiten&gt;
   &lt;/WartbareEinheit&gt;
  &lt;/WartbareEinheitenListe&gt;
  </code></pre>
  
  <p>My Code:</p>
  
  <pre><code>$Path = "$env:....\Desktop\xmlpath.xml"
  
  $xml = New-Object -TypeName XML
  $xml.Load($Path)
  
  $AlOb = $xml.WartbareEinheitenListe.WartbareEinheit.WartbareEinheiten.WartbareEinheit | Where-Object {$_.Customer -eq "100008"}
  
  $AlOb2 = $AlOb.SelectSingleNode("VIF")
  $R = $AlOb | Where-Object {$_.Typ -eq "OBJECT"}
  $E = $R.Typ
  
  for ( $i=0 ; $i -lt $E.count; $i++) {
  
  $InfoFeld = $xml.CreateElement("InfoFeld")
  
  $Mandant = $xml.CreateElement("Name2")
  $MandantInhalt = $xml.CreateTextNode("Reference")
  $Mandant.AppendChild($MandantInhalt)
  
  $InfoFeld.AppendChild($Mandant)
  
  $AlOb2.InsertAfter($InfoFeld,$AlOb.Node.VIF)
  }
  $NewPath = "$env:....Desktop\xmlpath2.xml"
  $xml.Save($NewPath)
  </code></pre>
  
  <p>The result is that I get two new Elements for every VIF, which is correct, but my code inserts both Elements in the last VIF node.</p>
  
  <p>Wheres my mistake?</p>