compare-object & copy-item help

Hi

Am trying to get a “small” script to work…

Basically I want to:

  1. compare 1 domain network share folder to 1 local folder
  2. if any new files are found on network share OR if any files have changed, then copy/overwrite.

I’ve been working on the below code but a few issues…

  1. the script copies files over but the folder in which the file is found…

so if filea.jpg is in folder “folder1”, then inside “folder1” I will find another “folder1” which is empty…
Also if a file is only found in the source then the script fails with:
Copy-Item : Cannot find path blablablabla\sourceonlyfile.jpg

Any thoughts to get me further?

$LocalFolder=“C:\Domain Presentation Solution”
$SourceFolder=“\domain.local\Marketing\LOGISTICS\Powerpoint\Domain Presentation Solution”

#$Source=Get-ChildItem $SourceFolder -recurse
#$Destination=Get-Childitem $LocalFolder -Recurse

#Leaving out folders… but what if there’s a new folder in source??
$Source=Get-ChildItem $SourceFolder -recurse | Where-Object {!($.PSContainer)}
$Destination=Get-Childitem $LocalFolder -Recurse | Where-Object {!($
.PSContainer)}

Compare-Object -ReferenceObject $Source -DifferenceObject $Destination -Property Name, LastWriteTime -PassThru | Where-Object {$.Sideindicator -eq "<="} |
ForEach-Object
{
if ($
.Name -ne "Start Presentation.ps1")
{Copy-Item "$SourceFolder$($.name)" -Destination "$LocalFolder$($.name)" -Force}
}

ok, spotted 1 error

Where-Object {!($_.PSContainer)}

should be

Where-Object {!($_.PSIsContainer)}

That takes care of the folders appearing…

but what about the “cannot find path” ?

Also… what if there’s a NEW folder in source? will it be created since ‘-Force’ parameter is used on copy-item?

I’m excluding the “start presentation.ps1” since that’s the file I am creating…

I’ll offer that Robocopy.exe would be much better, and much faster, and much more accurate. And it runs fine from within PowerShell.

However, please be careful in responding to your own post as the first response. That removes you from the “Topics With No Replies” list, which is what most of us use to find posts that still need attention.

Hi

Thanks … I’ve thought about robocopy but I’d really really like to keep it native to PS.

I’m using the below code now… and it’s working wonderfully but 1 issue… if I create 1 folder and then nest another folder inside it… it will create the first folder first, then create the first folder again inside the newly created folder… along with the nested folder…

any thoughts?


$file = $null
Compare-Object (Get-Directories $SourceFolder) (Get-Directories $Localfolder) -Property RelativePath, Name, Length |
Sort RelativePath, Name, Length -desc | % {
if ($file -ne $.RelativePath) { $ } } |
Where-Object {$.SideIndicator -eq "<="} |
ForEach-Object {
$file = $
.RelativePath
copy-item "$SourceFolder$file" "$LocalFolder$file" -WhatIf
}

(whatif is just while testing)

hm… removed the -desc from the “Sort” parameter… seems to do the trick…