Recursively Copy a Directory in PowerShell

Image Description

Daily PowerShell #41

Daily PowerShell

November 26, 2021

quote Discuss this Article

In this post, we look at how to recursively copy a directory.

Assume we have a folder structure stored in C:\source such as the following.

- 1
    - 1
    - 2
    - 3
- 2
    - 1
    - 2
        - 1
            - data.txt
    - data.txt

To copy the same folder structure to the C:\destination directory, you can do the following.

Copy-Item -Path C:\source\* -Destination C:\destination\ -Recurse -Container

The resulting destination folder will contain the same directory structure as the source folder.


    Directory: C:\destination

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          11/26/2021  5:32 PM                1
d----          11/26/2021  5:32 PM                2
d----          11/26/2021  5:32 PM                3

    Directory: C:\destination\2

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          11/26/2021  5:32 PM                1
d----          11/26/2021  5:32 PM                2
-a---          11/26/2021  5:29 PM              7 data.txt

    Directory: C:\destination\2\1

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          11/26/2021  5:29 PM              7 data.txt