How to Copy Folder With Subfolders in PowerShell

Rohan Timalsina Feb 02, 2024
  1. Copy Folder With Subfolders in PowerShell
  2. Use Copy-Item Cmdlet to Copy Folder With Subfolders in PowerShell
How to Copy Folder With Subfolders in PowerShell

This tutorial will teach you to copy folders with subfolders in PowerShell.

Copy Folder With Subfolders in PowerShell

Mostly, system admins have to carry out various files and folders operations. Copying folder content from one location to another is one of them.

PowerShell supports the handling of different folder operations in the system. Using the PowerShell console, you can create, copy, move, rename, and delete folders.

Copying folders is simple using the copy and paste option or drag and drop feature in a few clicks. But, it will take a long time when you have a lot of folders to copy to the various locations.

You can run the PowerShell scripts or commands to automate folder copying tasks. You might also have to copy a folder’s subfolders and contents when copying it.

Use Copy-Item Cmdlet to Copy Folder With Subfolders in PowerShell

The Copy-Item cmdlet copies an item from one location to another. You can use this cmdlet to copy the folder and its contents to the specified location.

You will need to provide the source and destination path to copy from one place to another. The -Path parameter specifies the path to the items to copy, and the -Destination parameter specifies where the items should be copied.

The Copy-Item cmdlet does not print any output by default. The -PassThru parameter returns an object representing the copied item.

Here is an example of copying the folder C:\test_folder1 to C:\New.

Copy-Item -Path "C:\test_folder1" -Destination "C:\New" -PassThru

Output:

    Directory: C:\New


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/23/2022  11:43 PM                test_folder1

It only copies the folder without its contents.

Get-ChildItem "C:\New"

Output:

 Directory: C:\New


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/23/2022  11:43 PM                test_folder1
-a----         2/3/2022  12:36 PM             81 python.txt
-a----         2/3/2022  12:36 PM             84 test.txt

You will need to use the -Recurse parameter to perform a recursive copy. It copies all the contents of a folder with subfolders to a specified directory.

The following command copies the contents of a folder C:\test_folder1 to the directory C:\pc.

Copy-Item -Path "C:\test_folder1" -Destination "C:\pc" -Recurse

Now, verify whether the contents of test_folder1 are copied or not.

Get-ChildItem "C:\pc\test_folder1"

Output:

    Directory: C:\pc\test_folder1


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/23/2022  11:22 PM                New folder
-a----         2/23/2022  10:29 PM              0 books.txt
-a----         2/23/2022  10:29 PM              0 hello.txt

As shown, all the folder contents with the subfolder are copied to the specified location.

If you only want to copy the contents of a folder but not the folder itself, you can use the \* in the source path.

For example:

Copy-Item -Path "C:\test_folder1\*" -Destination "C:\folder2" -Recurse

Check the contents in the directory C:\folder2.

Get-ChildItem "C:\folder2"

As shown, this time, the test_folder1 is not copied, but all of its contents are copied to the directory C:\folder2.

Output:

 Directory: C:\folder2


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2/23/2022  11:36 PM                New folder
-a----         2/23/2022  10:29 PM              0 books.txt
-a----         2/23/2022  10:29 PM              0 hello.txt

We hope this tutorial helps you copy folders with subfolders using PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Folder