Copy Sites Page From One Site To Another Using PNP PowerShell

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.
  1. try {  
  2.     $srcUrl = Read - Host "Enter the source site url"  
  3.     $destUrl = Read - Host "Enter the destination site url"  
  4.     $pageName = Read - Host "Enter the page name which you want to copy"  
  5.     $cred = Get - Credential  
  6.     Connect - PnPOnline - Url $srcUrl - Credentials $cred  
  7.     $tempFile = [System.IO.Path]::GetTempFileName();  
  8.     Export - PnPClientSidePage - Force - Identity $pageName - Out $tempFile  
  9.     Connect - PnPOnline - Url $destUrl - Credentials $cred  
  10.     Apply - PnPProvisioningTemplate - Path $tempFile  
  11.     Write - Host "ModernPage is successfully copied."  
  12.     sleep 10  
  13. catch {  
  14.     Write - Host - ForegroundColor Red 'Error '':'  
  15.     $Error[0].ToString();  
  16.     sleep 10  
  17. }  

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.
 Copy Sites Page From One Site To Another Using PNP PowerShell
  • Then it shows a popup asking for credentials. Enter your username and password then click on ok.
Copy Sites Page From One Site To Another Using PNP PowerShell
  •  Now your modern page is copied to your destination site.
Copy Sites Page From One Site To Another Using PNP PowerShell
 
Copy Sites Page From One Site To Another Using PNP PowerShell