In this blog, we are going to discuss how to copy a modern page from one site collection to another site collection using PNP PowerShell.
To successfully copy a modern page, you have to follow the below steps,
- PowerShell code
- Run the script
PowerShell code
Instruction
In this code, first we have to set the credential of the SharePoint. Then we export the Modern page and the page template from the source site and apply it to the destination site.
- try {
- $srcUrl = Read - Host "Enter the source site url"
- $destUrl = Read - Host "Enter the destination site url"
- $pageName = Read - Host "Enter the page name which you want to copy"
- $cred = Get - Credential
- Connect - PnPOnline - Url $srcUrl - Credentials $cred
- $tempFile = [System.IO.Path]::GetTempFileName();
- Export - PnPClientSidePage - Force - Identity $pageName - Out $tempFile
- Connect - PnPOnline - Url $destUrl - Credentials $cred
- Apply - PnPProvisioningTemplate - Path $tempFile
- Write - Host "ModernPage is successfully copied."
- sleep 10
- } catch {
- Write - Host - ForegroundColor Red 'Error ', ':'
- $Error[0].ToString();
- sleep 10
- }
Run the script
To get the proper result follow the below instructions,
- Run the script.
- Give the source site url from which you want to copy.
- Give the destination site url in which you want to copy the modern page.
- Type the page title which you want to copy.
- Then it shows a popup asking for credentials. Enter your username and password then click on ok.
- Now your modern page is copied to your destination site.