All I want to do is verify the file(s) that are in folder 1 are in folder 2 as well. All I need to verify is the file name. For test purposes, I know they are the same file(s), but I am not getting the results that they are the same. Also I need to exclude a *.txt file in folder 1, which I have had zero success at as well and not even included in my two examples. I know this should be pretty simple, but I am struggling. If it matters the zip files I am testing with are named N324RA_TEST__20240423221326.zip & N335RT_TEST_20250410020632.zip. These will be in folder 1 for sure, I need to verify that they are also in folder 2 or not. If one or both are not in folder 2, I need to know
I've tried:
$Temp = "C:\MaxTemp\"
$Temp2 = "C:\MaxTemp2\"
$files1 = Get-ChildItem -Path $Temp -Recurse | Where-Object { ! $_.PSIsContainer }
$files2 = Get-ChildItem -Path $Temp2 -Recurse | Where-Object { ! $_.PSIsContainer }
write-host "$files1"
write-host "$files2"
if ($fiiles1 -eq $files2)
{write-host "same"
}
else
{write-host "not same"}
This resulted in files are not the same. I've also tried this, but it finds files are the same no matter what.
$Temp = "C:\MaxTemp\"
$Temp2 = "C:\MaxTemp2\"
$files1 = Get-ChildItem -Path $Temp -Recurse | Where-Object { ! $_.PSIsContainer }
$files2 = Get-ChildItem -Path $Temp2 -Recurse | Where-Object { ! $_.PSIsContainer }
if ($comparison) {
Write-Host "Differences found:"
$comparison | ForEach-Object {
if ($_.SideIndicator -eq "<=") {
Write-Host "Only in $($Temp): $($_.Name)"
}
}
}
else{ ($_.SideIndicator -eq "==")
Write-Host "No differences found. Both folders contain the same files."
}
If I understand your question properly, you only want to get a message when Folder 2 misses some files that are in Folder 1 (including subfolders)
In that case try the code below:
$Temp1 = 'C:\MaxTemp'
$Temp2 = 'C:\MaxTemp2'
# your question only mentions .zip files, in that case I would use the -Filter parameter:
$files1 = (Get-ChildItem -Path $Temp1 -File -Filter '*.zip' -Recurse).Name
$files2 = (Get-ChildItem -Path $Temp2 -File -Filter '*.zip' -Recurse).Name
# if however you need more filetypes except for .txt files, do this instead:
# $files1 = (Get-ChildItem -Path $Temp1 -File -Exclude '*.txt' -Recurse).Name
# $files2 = (Get-ChildItem -Path $Temp2 -File -Exclude '*.txt' -Recurse).Name
# return the filename(s) that are found in folder1, but not in folder2
$missing = @($files1 | Where-Object { $files2 -notcontains $_ })
if ($missing.Count) {
$message = "Folder 2 does not contain file(s) {0}" -f ($missing -join ', ')
Write-Host $message -ForegroundColor Red
}
else {
Write-Host "Folder 2 contains the same files that are in Folder 1" -ForegroundColor Green
}