title: Search CSV with PowerShell
body: <p>I am trying to do simple task with CSV using the PowerShell but its really not working for me.</p>
  
  <p>I have CSV file like this.</p>
  
  <pre>
  Name    LineUri                     Status         BusinessUnit Location
  Test 1  tel:+61396176100;ext=6100   Spare          Sales        VIC
  Test 2  tel:+61396176101;ext=6101   Spare          Sales        VIC
  Test 2  tel:+61396176102;ext=6102   Spare          Support      NSW
  Test 2  tel:+61396176103;ext=6103   Used           WareHouse    SA
  Test 2  tel:+61396176104;ext=6104   Used           Sales        SA
  Test 2  tel:+61396176105;ext=6105   Spare          Support      VIC
  Test 2  tel:+61396176106;ext=6106   Spare          WareHouse    VIC
  Test 2  tel:+61396176107;ext=6107   Spare          Support      VIC
  </pre>
  
  <p>I am trying to search the LineUri whose status is "Spare" based on location.</p>
  
  <p>So criteria will be like this:</p>
  
  <pre>
  Status = "Spare" 
  BusinessUnit = "WareHouse"
  Location = "VIC"
  </pre>
  
  <p>This should return me "tel:+61396176106;ext=6106" as spare.</p>
  
  <p>Once I have this Spare LineUri, I have another logic that will be executed and then change the status of that particular LineUri to "Used".</p>
  
  <p>I have this code which is giving me the lineUri, but I am not sure how can I take out that return value from <code>ForEach-Object</code> loop and used in another function.</p>
  
  <pre><code>$path     = Split-Path -parent $MyInvocation.MyCommand.Definition
  $newpath  = $path + "\PhoneData.csv"
  # Array of spare numbers that meet your criteria
  $firstAvailableSpare
  
  # criteria
  $currentStatus = "Spare"
  $userBusinessUnit = "WareHouse"
  $userLocation = "VIC"
  
  $csv = Import-Csv $newpath -Delimiter ","
  
  $csv | ForEach-Object {
      $Status += $_.Status
      $BusinessUnit += $_.BusinessUnit
      $Location += $_.Location
  
      if (($_.Status -eq $currentStatus) -and ($_.BusinessUnit -eq $userBusinessUnit) -and ($_.Location -eq $userLocation)) { 
          $firstAvailableSpare = $_ # Assign the first
          break # break the loop so we don't get additional hits
      }
  }
  
  try {
      #use the return value and update the csv so that its status is changed to Used
  } catch {
      #do something else if failed
  }
  </code></pre>