title: foreach loop through a line from a csv file in powershell not working
body: <p>I have a script I have been working on to generatie some xml files it works as expected when I run it with only 2 rows in the CSV when I add more rows it just combines the values instead of looping. Example the last line is to save the file with 12345678.xml file and instead names it with the numbers from all rows. example
  12345678
  25789654
  45658965
  46982268.xml
  It does this with all lines in the loop.
  I need help getting to loop through each row and making 1 file per row instead of combing the data from all rows. It should loop 4 times saving 4 files instead of 1 file with all the data combined.</p>
  <pre><code> #Define the source CSV with the list of MAC address and Phone Numbers 
  #$sourceCSV = Import-Csv &quot;C:\users\bob\Desktop\phones.csv&quot; 
  
   
  $xmltemp = Get-Content C:\users\bob\Desktop\xmltemp.xml
  
  
  #Pull first 13 lines of the XML Temp as variable chunk1
  $chunk1 = $xmltemp | Select-Object -First 11
  
  #Select lines 15 to 20 of the XML Temp as variable chunk2
  $chunk2 = $tdcxmltemp | Select-Object -First 6 -skip 10
  
  #Select lines 22 to 116 of the XML Temp as variable chunk3
  $Chunk3 = $tdcxmltemp | Select-Object -First 95 -skip 11
  
  #Select lines 120 to eof of the XML Temp as variable chunk4
  $Chunk4 = $tdcxmltemp | Select-Object  -skip 119
  
  
  
  #Loop to combine the data, and generate a MACaddress.xml for each MAC listed in the CSV file
  Import-Csv &quot;C:\users\bob\Desktop\phones.csv&quot; | ForEach-Object {
  
      foreach ($phone in $_.PSObject.Properties.mac){
  
              
                  $phonenum = $sourceCSV.number
                  $phonemac = $sourceCSV.mac
                  $update = $sourceCSV.update_server
                      
  
  # combine the first 13 lines of the script and Append phone number on line 13, add lines 15-20 into variable &quot;xml2&quot;
  $xml2 = $chunk1 + (&quot;&lt;!-- Configuration file for Line &quot; + $phonenum + &quot; --&gt;&quot;) + $chunk2
  
  
  #Add Update server to xml2 variable and create xml3 variable, combine with chunk 3
  $xml3 = $xml2 + ('          &lt;update_server source=&quot;STATIC&quot;&gt;' + $updater + &quot;&lt;/update_server&gt;&quot;) + $Chunk3
  
  
  #combine xml3 veriable with line 117 - append phone number (line_id) into variable xml4
  $xml4 = $xml3 + ('              &lt;line_id&gt;' + $phonenumr + &quot;&lt;/line_id&gt;&quot;) 
  
  
  #combine xml4 veriable with line 118 - append phone number (label) into variable xml5
  $xml5 = $xml4 + ('              &lt;label&gt;' + $phonenum + &quot;&lt;/label&gt;&quot;)
  
  
  #combine xml5 veriable with line 119 - append phone number (sip_name) into variable xml6
  $xml6 = $xml5 + ('              &lt;sip_name&gt;' + $phonenumr + &quot;&lt;/sip_name&gt;&quot;)
  
  
  #Combine xml6 veriable with chunk4 into variable xml7
  $xml7 = $xml6 + $chunk4 
  
  
  #save the xml file and name with macadress 
  $xml7 | Out-File ('C:\users\bob\Desktop\'  + $phonemac  + '.xml')
  }
  }
  </code></pre>
  <p>CSV FIle</p>
  <pre><code>    mac,number,update_server
  12345678,444-2223,192.168.1.1
  25789654,1234567,192.168.1.1
  45658965,7654321,192.168.1.1
  46982268,99999999,192.168.1.1
  </code></pre>