PowerShell Copy-Item with Folder Structure

In this tutorial, I will explain how to use PowerShell’s Copy-Item cmdlet to copy files and folders while maintaining their original structure. This is a common task for system administrators and developers who need to manage file systems efficiently. You will learn here how to use the PowerShell Copy-Item cmdlet to efficiently copy files and folders from one location to another while maintaining the original directory structure.

I will also show you another example of creating the folder structure while copying files using PowerShell Copy-Item.

PowerShell Copy-Item with Folder Structure

In my previous tutorial, I explained about the PowerShell Copy-Item cmdlet.

The Copy-Item cmdlet in PowerShell allows you to copy an item from one location to another within the same namespace, such as copying files to a folder. It provides a simple and efficient way to duplicate files and directories.

Here’s the basic syntax of the Copy-Item cmdlet:

Copy-Item -Path <source> -Destination <target> [options]
  • <source>: The path to the file or folder you want to copy.
  • <target>: The path to the destination folder where the item will be copied.
  • [options]: Additional parameters to control the copying behavior.

To copy files from one directory to another while keeping the original folder structure intact, you can use the -Recurse and -Container parameters with Copy-Item PowerShell cmdlet.

Here’s an example:

$sourceDir = "C:\MyFolder"
$targetDir = "D:\Reports"
Get-ChildItem -Path $sourceDir | Copy-Item -Destination $targetDir -Recurse -Container

In this script:

  1. We define the $sourceDir variable as the path to the source directory containing the files and folders we want to copy.
  2. We specify the $targetDir variable as the path to the destination directory where the copied items will be placed.
  3. We use Get-ChildItem to retrieve all the files and subdirectories within the source directory.
  4. We pipe the results to Copy-Item and provide the destination directory path.
  5. The -Recurse parameter ensures that all subdirectories and their contents are copied recursively.
  6. The -Container parameter preserves the directory structure during the copy operation.

You can see the exact output in the screenshot below after I executed the above PowerShell script.

PowerShell Copy-Item with Folder Structure

Check out Get-ChildItem Sort By Date in PowerShell

Copy Specific Files While Maintaining Folder Structure

If you only need to copy specific files within a directory structure, you can use a combination of Get-ChildItem with filtering and Copy-Item.

Here’s an example that copies all PDF files from the source directory to the destination while keeping the folder structure:

$sourceDir = "C:\Documents"
$targetDir = "C:\Archives"
Get-ChildItem -Path $sourceDir -Include *.pdf -Recurse | Copy-Item -Destination $targetDir -Container

In this script:

  • We set the $sourceDir variable to the path of the directory containing the files we want to copy.
  • We define the $targetDir variable as the path to the destination directory.
  • We use Get-ChildItem with the -Include parameter to filter and retrieve only the PDF files within the source directory and its subdirectories using the -Recurse parameter.
  • We pipe the filtered files to Copy-Item and specify the destination directory.
  • The -Container parameter ensures that the directory structure is preserved during the copy operation.

This approach allows you to selectively copy specific file types while maintaining their original folder structure.

Read How to Get File Size Using PowerShell?

Handle Duplicate Files and Overwriting

By default, Copy-Item will overwrite files in the destination directory if they already exist and have the same name as the source files. If you want to prevent overwriting and skip copying duplicate files, you can use the -Force parameter.

Here’s an example:

$sourceDir = "C:\Projects\App"
$targetDir = "D:\Backup\App"
Get-ChildItem -Path $sourceDir | Copy-Item -Destination $targetDir -Recurse -Container -Force

In this script:

  • We specify the source directory in the $sourceDir variable.
  • We set the destination directory in the $targetDir variable.
  • We use Get-ChildItem to retrieve all files and subdirectories from the source directory.
  • We pipe the results to Copy-Item and provide the destination directory.
  • The -Recurse and -Container parameters ensure that the directory structure is preserved during copying.
  • The -Force parameter skips copying duplicate files and prevents overwriting existing files in the destination.

By using the -Force parameter, you can avoid accidentally overwriting important files in the destination directory.

Read Get the Last Modified Date of a File in PowerShell

PowerShell Copy-Item Create Folder Structure

Now, let me show you another requirement that is similar. Sometimes, you may need to create a folder structure while copying files using the PowerShell Copy-Item cmdlet. Let me help you with an example.

Let’s say you want to copy a file named “report.pdf” from a source folder “C:\Projects\Reports” to a destination folder “D:\Archives\2024\Reports” while creating the necessary folder structure in the destination if it doesn’t already exist. You can use the PowerShell Copy-Item cmdlet along with the Split-Path and New-Item cmdlets to achieve this.

Here’s an example PowerShell script:

$sourceFile = "C:\Projects\Reports\report.pdf"
$destinationFolder = "D:\Archives\2024\Reports"

# Create the destination folder structure if it doesn't exist
$destinationPath = Split-Path -Path $destinationFolder
if (-not (Test-Path -Path $destinationPath)) {
    New-Item -ItemType Directory -Path $destinationPath | Out-Null
}

# Copy the file to the destination folder
Copy-Item -Path $sourceFile -Destination $destinationFolder

Explanation:

  • We define the $sourceFile variable as the path to the file we want to copy, which is “C:\Projects\Reports\report.pdf”.
  • We specify the $destinationFolder variable as the path to the destination folder where we want to copy the file, which is “D:\Archives\2024\Reports”.
  • We use the Split-Path cmdlet to extract the parent directory path from the destination folder. In this case, it will be “D:\Archives\2024”.
  • We check if the destination path exists using the Test-Path cmdlet. If it doesn’t exist, we use the New-Item cmdlet with the -ItemType Directory parameter to create the necessary folder structure.
    • The Out-Null cmdlet is used to suppress the output of New-Item.
  • Finally, we use the Copy-Item cmdlet to copy the file from the source path to the destination folder.

After running this script, the “report.pdf” file will be copied from “C:\Projects\Reports” to “D:\Archives\2024\Reports”, and the necessary folder structure will be created in the destination if it doesn’t already exist.

Conclusion

In this tutorial, I explained how to use the PowerShell Copy-Item cmdlet to copy files and folders while preserving the original directory structure. I explained different scenarios, such as copying entire directories, filtering specific file types, and handling duplicate files.

By using the -Recurse and -Container parameters with Copy-Item, you can easily duplicate complex file systems while maintaining their folder structure. This technique is particularly useful when backing up projects, archiving documents, or migrating files between different locations. I hope this helps.

You may also like:

>