logo
search
SharePoint Document Issues

How to Copy Selected SharePoint Subfolders Recursively with PowerShell

Bushra ParveenBushra Parveen Sep 30, 2026 868 views

Question details

The user needs a reliable method to copy specific subfolders from multiple SharePoint parent directories while maintaining the original document hierarchy.

How to Copy Selected SharePoint Subfolders Recursively Using PowerShell
Product
Microsoft SharePoint
Device & OS
not provided
Scenario
Automating the structural migration or backup of specific document directories across SharePoint sites using custom scripts.
Observed behavior
Requires a recursive PowerShell script with a folder-name filter to securely copy target folders without losing structure or essential metadata.
Before you start

Ensure you have the required SharePoint Administrator permissions and the SharePoint Online Management Shell installed. Always test custom recursive scripts on a non-production site first to avoid unintended data loss or duplication.

Solution 1Recommended

Develop a Custom PnP PowerShell Script

Use PnP PowerShell modules to iterate through parent folders, filter for your target subfolders, and copy them recursively.

Automating SharePoint folder duplication requires custom scripting. Leveraging PnP PowerShell modules allows you to interact directly with your site's document libraries, filter folders by specific names, and recursively copy files to a new destination while preserving folder structures.

1
Install PnP PowerShell

Open your PowerShell console as an Administrator and run the command `Install-Module PnP.PowerShell` to install the necessary modules for SharePoint online management.

2
Connect to SharePoint Site

Authenticate and connect to your source SharePoint environment using the command `Connect-PnPOnline -Url "https://yourdomain.sharepoint.com/sites/yoursite" -Interactive`.

3
Filter and Iterate Subfolders

Write a script that uses `Get-PnPFolderItem` to iterate through the document library. Use a `Where-Object` clause to filter the results by your specific folder name criteria.

4
Execute the Copy Command

Utilize the `Copy-PnPFile` cmdlet within a recursive loop to migrate the identified folders and their nested contents to the new destination.

Develop a Custom PnP PowerShell Script
Preserving Metadata: Basic PowerShell copy commands often reset metadata such as 'Created By' and timestamps. If retaining exact author and version history is strictly required, standard cmdlets may need to be substituted with a dedicated SharePoint migration tool.
Free Microsoft Office alternative

Streamline Your Local Document Management with WPS Office

While migrating complex SharePoint libraries requires advanced PowerShell scripting and technical expertise, your everyday document editing should be simple and hassle-free. WPS Office provides a robust, lightweight, and user-friendly alternative to Microsoft Office, letting you focus on your work without complex server configurations.

Seamless format compatibility with Microsoft Word, Excel, and PowerPoint files (.docx, .xlsx, .pptx).Completely free and lightweight, ensuring fast load times even on older hardware.Familiar user interface that requires zero learning curve for users switching from Microsoft Office.Built-in cloud sync for easy and secure document management without the complexity of SharePoint.
microsoft office alternative - wps office

Frequently Asked Questions

Can I copy SharePoint subfolders recursively without using PowerShell?

Yes, for smaller libraries, you can select the folders in the SharePoint web interface and click 'Copy to' from the top menu. Alternatively, you can sync the SharePoint library to your PC using OneDrive and copy the folders using Windows File Explorer. However, these methods are manual and can struggle with large-scale structures.

Will copying folders via PowerShell preserve file version history?

Generally, no. Standard PowerShell cmdlets like Copy-PnPFile create a new instance of the file at the destination, which resets version history and typically changes the 'Created By' and 'Modified By' fields to the user running the script. If version history is crucial, consider using the official SharePoint Migration Tool (SPMT).

How do I ensure my PowerShell script only copies folders and not files?

When retrieving items using 'Get-PnPFolderItem', you can append the parameter '-ItemType Folder' to your command. This ensures the script only loops through directory objects and ignores loose files within the parent directory before applying your name filters.