You chose misleading variable names and fell into the hole you dug yourself.
$Folder1 = Get-childitem "C:\one\"
$Folder2 = Get-childitem "C:\two\"
$Folder3 = Get-childItem "C:\three\"
These instructions will fill the variables with the child items of the given folders.
Copy-Item "$Folder1\$($_.name)" -Destination $Folder3 -Force
This instruction, however, uses $Folder1
and $Folder3
as if they contained the folder path (which they don't).
On top of that, your code will fail, because Compare-Object -Property Name, Length
will always produce web_rollback.config
as the result for the side indicator =>
(since the names of the items in C:\one
and C:\two
are different even if the file sizes aren't), and no file with that name is present in C:\one
.
Another flaw with your approach is that you're relying on a difference in size for detecting a change between the two files. This check will fail if for instance a value was changed from 0
to 1
.
Change your code to something like this:
$config = "C:\one\web.config"
$rollback = "C:\two\web_rollback.config"
$target = Join-Path "C:\three" (Get-Item $config).Name
if ([IO.File]::ReadAllText($config) -ne [IO.File]::ReadAllText($rollback)) {
Copy-Item $rollback -Destination $target -Force
}