title: Find a file in a collection of files
body: <p>I'm using PowerShell to compare two directories of file names. I can make it work iterating through <code>$dir1</code> and looking up each file in <code>$dir2</code> but it is understandably "slow" for 500 files across a network. </p>
  
  <p>I would like to capture all the files in a collection using <code>$files = gci $dir1</code>, iterate through <code>$dir2</code> and search the collection for each file. </p>
  
  <p>I tried: </p>
  
  <pre><code>$files = gci $dir1
   gci $dir2 | foreach {
     if ($files -contains $_) {
        echo "found" }
     } 
  </code></pre>
  
  <p>and, of course, this was too easy.  </p>
  
  <p>What is the best way to do this search in a reasonably speedy way?  </p>
  
  <p>After reading @Mathias answer I switched from using <code>Select Name</code> to <code>gci $dir1 -Name</code> and now the contains works. Of course this changes the question since I am no longer dealing with a collection of file objects, just strings. 
  Since I'm looking for missing files in $dir2, the final code looks like this:</p>
  
  <pre><code>$files = gci $dir2 -name
  $count = 0
       gci $dir1 -name | foreach {
          $fname = $_
         if ($files -notcontains $fname) {
            echo "not found: $fname" 
            $count +=1 }
         } 
      $count
  </code></pre>