PowerTip: Duplicate File Structure with PowerShell

Summary: Use Copy-Item to duplicate content with Windows PowerShell.

Hey, Scripting Guy! Question How can I use Windows PowerShell to duplicate content?

Hey, Scripting Guy! Answer Use Copy-Item—for example, to duplicate all files and folders from C:\Foo to D:\Foo, run:

Copy-Item C:\Foo D:\Foo -recurse

Comments (9)

  1. me says:

    What?? Reread what you posted please.

  2. Jack says:

    You’ve got "Get-ChildItem" instead of "Copy-Item" in your example command…

  3. JVimes says:

    Also, note that if d:foo already exists, it won’t overlay the source file tree on it. Rather, it will nest it: d:foofoo.

  4. @me @jack @jvimes

    Sorry about that. Pasted the wrong line from a different script.

    Copy-item C:Foo D:Foo -recurse

    And you are most correct, if D:Foo is already present it will drop in a new folder called D:FooFoo under D:Foo

    Must learn to stay away from that coffee!


  5. alz dba says:

    Is there a way to force it to include ACLs?

  6. @alz dba

    There are many ways (*including a really cool module on the TechNet Script Repository for managing ACLs!*)

    Top of my head you might trying something like

    $Get-childitem c:foo -recurse | Get-ACL | Export-clixml foosecurity.xml

    Restoring would be a matter of a combination of using Import-Clixml foosecurity.xml to get the Security ACLS, Parsing the old file path and swapping the new, and Set-ACL.

    The second part is all theory too as you just caught me heading out the door. I’l have to play with that for a Weekend Scripting Guy article for fun. 🙂


  7. I_am_mr_ed says:

    Updated the code in the PowerTip. Thanks for all your comments.

  8. angel ikaz says:

    i would suggest you to try DuplicateFilesDeleter , it can help resolve duplicate files issue.

Skip to main content