compare-object & copy-item help

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Martin Thomsen Martin Thomsen 10 months, 3 weeks ago.

  • Author
  • #35928
    Profile photo of Martin Thomsen
    Martin Thomsen


    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\$($" -Destination "$LocalFolder\$($" -Force} }

  • #35929
    Profile photo of Martin Thomsen
    Martin Thomsen

    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?

  • #35930
    Profile photo of Martin Thomsen
    Martin Thomsen

    I'm excluding the "start presentation.ps1" since that's the file I am creating..

  • #35931
    Profile photo of Don Jones
    Don Jones

    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.

  • #35964
    Profile photo of Martin Thomsen
    Martin Thomsen


    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)

  • #35965
    Profile photo of Martin Thomsen
    Martin Thomsen

    hm.. removed the -desc from the "Sort" parameter.. seems to do the trick...

You must be logged in to reply to this topic.