How to Copy Selected SharePoint Subfolders Recursively with PowerShell
Question details
The user needs a reliable method to copy specific subfolders from multiple SharePoint parent directories while maintaining the original document hierarchy.

- 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.
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.
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.
Open your PowerShell console as an Administrator and run the command `Install-Module PnP.PowerShell` to install the necessary modules for SharePoint online management.
Authenticate and connect to your source SharePoint environment using the command `Connect-PnPOnline -Url "https://yourdomain.sharepoint.com/sites/yoursite" -Interactive`.
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.
Utilize the `Copy-PnPFile` cmdlet within a recursive loop to migrate the identified folders and their nested contents to the new destination.

Consult the SharePoint Development Community
Seek expert advice and verified script templates from SharePoint developers to ensure a safe migration.
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.

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.




