title: Powershell comparing two List with foreach
body: <p>I'm trying to compare two lists that I obtained with the <code>Get-ChildItem</code> cmdlet.</p>
  
  <p>I read about the <code>Compare-Object</code> cmdlet, but in this context I would prefer doing with a foreach because is useful to have access to the <code>$file</code> variable.</p>
  
  <pre><code>$StartingFolderPath = "C:\---\StartingFolder"
  $EndingFolderPath = "C:\---\EndingFolder"
  
  
  $AllStartingFiles = Get-ChildItem $StartingFolderPath 
  $AllEndingFiles = Get-ChildItem $EndingFolderPath
  
  Write-Host "First folder content:"$AllStartingFiles 
  Write-Host "Second folder content:" $AllEndingFiles  
  
  
  foreach($file in $AllEndingFiles){
  
      write-Host "Element :" $file.Name
  
      $result = $AllStartingFiles.Contains($file.Name)
      write-host $result
  
      if($AllStartingFiles.Contains($file.Name)){
      Write-Host  "You are here"
      Write-Host $file.Name
      }    
  }
  </code></pre>
  
  <p>But it seems that I cannot pass the if control, <code>if($AllStartingFiles.Contains($file.Name))</code> which returns false.</p>
  
  <p>OUTPUT</p>
  
  <pre><code>First folder content: 1_one.txt 2_two.txt 3_three.txt 5_five.txt
  Second folder content: 1_one.txt 2_two.txt 3_three.txt 4_four.txt 5_five.txt 
  6_six.txt
  Element : 1_one.txt
  False
  Element : 2_two.txt
  False
  Element : 3_three.txt
  False
  Element : 4_four.txt
  False
  Element : 5_five.txt
  False
  Element : 6_six.txt
  False
  </code></pre>
  
  <p>I've also tried with the <code>-Contains</code> operator, but without any luck.</p>