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:
- We define the
$sourceDirvariable as the path to the source directory containing the files and folders we want to copy. - We specify the
$targetDirvariable as the path to the destination directory where the copied items will be placed. - We use
Get-ChildItemto retrieve all the files and subdirectories within the source directory. - We pipe the results to
Copy-Itemand provide the destination directory path. - The
-Recurseparameter ensures that all subdirectories and their contents are copied recursively. - The
-Containerparameter 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.

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
$sourceDirvariable to the path of the directory containing the files we want to copy. - We define the
$targetDirvariable as the path to the destination directory. - We use
Get-ChildItemwith the-Includeparameter to filter and retrieve only the PDF files within the source directory and its subdirectories using the-Recurseparameter. - We pipe the filtered files to
Copy-Itemand specify the destination directory. - The
-Containerparameter 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
$sourceDirvariable. - We set the destination directory in the
$targetDirvariable. - We use
Get-ChildItemto retrieve all files and subdirectories from the source directory. - We pipe the results to
Copy-Itemand provide the destination directory. - The
-Recurseand-Containerparameters ensure that the directory structure is preserved during copying. - The
-Forceparameter 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
$sourceFilevariable as the path to the file we want to copy, which is “C:\Projects\Reports\report.pdf”. - We specify the
$destinationFoldervariable as the path to the destination folder where we want to copy the file, which is “D:\Archives\2024\Reports”. - We use the
Split-Pathcmdlet 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-Pathcmdlet. If it doesn’t exist, we use theNew-Itemcmdlet with the-ItemType Directoryparameter to create the necessary folder structure.- The
Out-Nullcmdlet is used to suppress the output ofNew-Item.
- The
- Finally, we use the
Copy-Itemcmdlet 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:
- PowerShell create log file
- PowerShell create file if not exists
- PowerShell check if file modified in last 24 hours

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.