title: Creating new columns in CSV based on recursive search
body: <p>I'm trying to process a 15MB CSV file that has only three columns. For example:</p>
  
  <pre class="lang-none prettyprint-override"><code>StaffNumber,EmailAddress,Manager
  123,ArthurDent@beeblebrox.com,456.
  </code></pre>
  
  <p>I need to search each line of the CSV file, display the staff number, email address, then take the Manager's ID number and search for that, displaying their staff number and e-mail, then take <em>their</em> manager's ID and repeat the same. In the end, I need to have each row of the CSV file have the user's ID and e-mail, and <strong>up to three managers ID's and e-mails</strong>.</p>
  
  <p>I'm trying to end up with this:</p>
  
  <pre class="lang-none prettyprint-override"><code>123,ArthurDent@beeblebrox.com,456,Marvin@beeblebrox.com,789,Zaphod@Beeblebrox.com,098,zaphod@beeblebrox.com
  </code></pre>
  
  <p>Some rows don't contain an e-mail address or manager's ID number, so that throws another wrench into the works. I was planning on adding simply "Blank" or "X" to those fields.</p>
  
  <p>So far, I've hammered out this simple script to search one at a time. It works, but it's atrociously slow. Since this morning, it's taken 6 hours to get 0.31% through that file. Ugh!</p>
  
  <p>I've read until I thoroughly confused myself about how best to handle 15MB worth of CSV data, different PowerShell versions, what's best this week, etc, and PowerShell is far from my usual language (take pity on my lack of PS skills. I was just trying to flesh out a basic test script to get an idea).</p>
  
  <p>I'm currently running PowerShell v4, and I know there are compatibility issues with earlier versions. I'd like to try to stay as compatible with future versions as reasonable.</p>
  
  <p>What would be the fastest way to process this file?
   At only 15MB for 300,000 lines, I'm not concerned about RAM. I just have no idea how to more efficiently run this search.</p>
  
  <pre><code>$FilePath = "C:\Temp\DA-UserList.csv"
  $DAUserlist = Import-CSV $FilePath
  
  $inputNumber = Read-Host -Prompt "Employee ID Number"
  
  $DAUser1 = $DAUserlist | Where{$inputNumber -match $_.StaffNumber}| Select -First 1
  ForEach ($item in $DAUser1){
      $StaffNumber1 = $($item.StaffNumber)
      $EmailAddress1 = $($item.EmailAddress)
      $Manager1 = $($item.Manager)
      printf $item.StaffNumber
      printf ","
      printf $EmailAddress1
      $DAUser2 = $DAUserlist | Where{$Manager1 -match $_.StaffNumber}| Select -First 1
      ForEach ($item in $DAUser2){
          $StaffNumber2 = $($item.StaffNumber)
          $EmailAddress2 = $($item.EmailAddress)
          $Manager2 = $($item.Manager)
          printf ","
          printf $StaffNumber2
          printf ","
          printf $EmailAddress2
          $DAUser3 = $DAUserlist | Where{$Manager2 -match $_.StaffNumber}| Select -First 1
          ForEach ($item in $DAUser3){
              $StaffNumber3 = $($item.StaffNumber)
              $EmailAddress3 = $($item.EmailAddress)
              $Manager3 = $($item.Manager)
              printf ","
              printf $StaffNumber3
              printf ","
              printf $EmailAddress3
              $DAUser4 = $DAUserlist | Where{$Manager3 -match $_.StaffNumber}| Select -First 1
              ForEach ($item in $DAUser4){
                  $StaffNumber4 = $($item.StaffNumber)
                  $EmailAddress4 = $($item.EmailAddress)
                  $Manager4 = $($item.Manager)
                  printf ","
                  printf $StaffNumber4
                  printf ","
                  printf $EmailAddress4
                  printf \n
              }
          }
      }
  }
  </code></pre>