Say I have a folder called '1' which has various folders in it. For example's sake to make it easy, say I have folders named 'A' through 'N' in alphabetical order inside that folder '1'. Then I have another folder called '2' in which it also has various folders in it, but it has, for example, only the folders 'B', 'C', 'F', 'K', and 'L'.

How can I automatically select the folders 'B', 'C', 'F', 'K', and 'L' in folder '1' and copy all of its contents/subfolders+its contents, etc. into folder '2' (but skip all existing files in '2' and just add the stuff that doesn't exist yet in '2' but '1' does have)?


This will get you started:

$targetdirs=Get-ChildItem folder2 | ? { $_.PSIsContainer }

foreach($dir in $targetdirs) { Copy-Item folder1\$dir folder2\$dir -Recurse}

There's only one problem, which I could not solve. The target folders get doubled. That is, you end up with Folder2\B\B instead of just Folder2\B. Maybe someone elase can improve on it.

  • I just removed from folder2\$dir the $dir part and it worked fine. Thanks – user152294 May 10 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.