I am trying to write a script that will generate an array of file names from one (origin) directory, generate another array from a different (destination) directory. Compare the (2) arrays of file name and output a list of files that are common to both directories. I want to export the filename and destination file path. I originally thought this would be pretty easy to do, but it has of course proved challenging. My unc paths are extremely long which necessitated the use of psdrives. During my troubleshooting I thought the spaces in the file names may have been causing problems so I removed the spaces. The use of -contains in the if statement seems to be a problem. I have confirmed data in variables is string data with spaces removed however, -contains still does not work correctly. I get no error messages, just nothing out of the if statement. If I swap -contains for -match the script works, but with invalid data. I need to match full file names and -contains seems to be the operator that should be used. I’m not sure why this is not working. Help please.
new-psdrive -Name O -PSProvider FileSystem -Root "unc path of original directory" new-psdrive -Name Des -PSProvider FileSystem -Root "unc path of destination directory" $Directory1 ="O:\" $Directory2 ="Des:\" Get-ChildItem -File -Recurse $Directory1 | Export-Csv -NoTypeInformation pathname Get-ChildItem -File -Recurse $Directory2 | Export-Csv -NoTypeInformation pathname $FilePath1 = import-csv -Path "path to file" | Select-Object -Property Name,Fullname $FilePath2 = import-csv -Path "path to file" | Select-Object -Property Name,Fullname $CommonFiles = Compare-Object -ReferenceObject $FilePath2 -DifferenceObject $FilePath1 -IncludeEqual -Property Name | where-object {$_.SideIndicator -eq "=="} | Select-Object -Property Name | Sort-Object -Property Name | Out-String $CommonFilesFormatted = $CommonFiles -replace " ","" foreach ($file in $FilePath2) { $FileFormatted = $file.Name.ToString() -replace " ","" If ($CommonFilesFormatted -contains $FileFormatted) { write-host $file "$file.name" + "$file.fullname" | Out-File path -append } Else { write-host "False"} }