Powershell comparing two List with foreach

Multi tool use


Powershell comparing two List with foreach
I'm trying to compare two lists that I obtained with the Get-ChildItem
cmdlet.
Get-ChildItem
I read about the Compare-Object
cmdlet, but in this context I would prefer doing with a foreach because is useful to have access to the $file
variable.
Compare-Object
$file
$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
}
}
But it seems that I cannot pass the if control, if($AllStartingFiles.Contains($file.Name))
which returns false.
if($AllStartingFiles.Contains($file.Name))
OUTPUT
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
I've also tried with the -Contains
operator, but without any luck.
-Contains
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.