SharePoint Online: Copy Site Pages from One Site to Another using PowerShell

Requirement: Copy site pages to another site in SharePoint Online.

SharePoint Online: How to Copy a Page to Another Site Collection?

Do you need to copy a page from one SharePoint Online site collection to another? Maybe you want to duplicate a page for testing purposes or create a similar page in another site collection. This article will show you how to copy pages in SharePoint Online using PowerShell.

We wanted to copy a modern page from one site to another site in SharePoint Online. The “Copy to” option doesn’t support copying site pages from one site to another.

sharepoint online copy page to another site collection

Luckily we’ve PnP PowerShell available to copy a modern page to another site:

#Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/marketing"
$DestinationSiteURL = "https://crescent.sharepoint.com/sites/branding"
$PageName =  "About.aspx"

#Connect to Source Site
Connect-PnPOnline -Url $SourceSiteURL -Interactive

#Export the Source page
$TempFile = [System.IO.Path]::GetTempFileName()
Export-PnPPage -Force -Identity $PageName -Out $TempFile

#Import the page to the destination site
Connect-PnPOnline -Url $DestinationSiteURL -Interactive
Invoke-PnPSiteTemplate -Path $TempFile

This PowerShell script export-imports a given page and all its web parts, contents, etc. How about copying all pages from one site to another?

PowerShell to Copy All Site Pages to Another Site in SharePoint Online

To copy all pages to another site collection, use this PowerShell script.

#Parameters
$SourceSiteURL = "https://crescent.sharepoint.com/sites/marketing"
$DestinationSiteURL = "https://crescent.sharepoint.com/sites/branding"

#Connect to the Source Site
Connect-PnPOnline -Url $SourceSiteURL -Interactive

#Export all pages from the source
$TempFile = [System.IO.Path]::GetTempFileName()
Get-PnPSiteTemplate -Out $TempFile -Handlers PageContents -IncludeAllClientSidePages -Force

#Import the page to the destination site
Connect-PnPOnline -Url $DestinationSiteURL -Interactive
Invoke-PnPSiteTemplate -Path $TempFile

This will backup all pages in the “Site Pages” library to an XML file with all the artifacts from pages and import them to the given destination site. We can use this method to backup-restore site pages within the same tenant or between different tenants in SharePoint Online.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

10 thoughts on “SharePoint Online: Copy Site Pages from One Site to Another using PowerShell

  • This is no longer working. Please update script

    Reply
  • I’ve got version 1.10.0 of Pnp.Powershell installed and am getting the following error when I run a script which I am attempting to get details of a customised modern SharePoint Teams Site Home Page to use as a template for the creation of other Sites’ Home Pages.

    Export-PnPPage : Object reference not set to an instance of an object
    At C:\Users\SteveWedge\OneDrive\Powershell\SharePoint\CopyITTeamSiteHomePage.ps1:11 char:1
    + Export-PnPPage -Force -Identity $PageName -Out $TempFile -PersistBran …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Export-PnPPage], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,PnP.PowerShell.Commands.Provisioning.Tenant.ExportPage

    The script I am running is not complex and at one point today did complete execution and produce some output, although the file which was created did not actually include the full definition of the page, webparts, etc. (see attached) so wasn’t very helpful.

    Since that 1 successful execution of the Export-PnPPage cmdlet, every subsequent attempt has failed. I’d rather not have to build out the other 10 Site Home pages manually, so am hoping you can shed some light on what’s wrong with this, or suggest an alternative approach.

    $SourceSiteURL = “https://companyname.sharepoint.com/teams/XYZ-GLBL-IT-Infrastructure-Operations”
    $DestinationSiteURL = “https://companyname.sharepoint.com/teams/XYZ-GLBL-IT-Infrastructure-EMEA”
    $PageName = “Home.aspx”
    #Connect to Source Site
    Connect-PnPOnline -Url $SourceSiteURL -Interactive
    #Export the Source page
    $TempFile = [System.IO.Path]::GetTempFileName()
    Export-PnPPage -Force -Identity $PageName -Out $TempFile -PersistBrandingFiles
    #Import the page to the destination site
    [tmp2AA9.txt](https://github.com/pnp/powershell/files/8622395/tmp2AA9.txt)

    Connect-PnPOnline -Url $DestinationSiteURL -Interactive
    Invoke-PnPSiteTemplate -Path $TempFile

    Reply
  • WARNING: Please use ‘Export-PnPPage’. The alias ‘Export-PnPPage’ will be removed in the 1.5.0 release

    Reply
  • script work great. However, can you include all the version history?

    Reply
  • Hi there,

    sorry to say but this page needs to be updated.
    Get-PnPProvisioningTemplate is not existing anymore in PnP.Powershell

    Kind regards,
    Dave

    Reply
    • Yes! If you have upgraded to New PnP PowerShell module, PnP.PowerShell, Get-PnPProvisioningTemplate cmdlet is replaced with “Get-PnPSiteTemplate” and “Apply-PnPProvisioningTemplate” is replaced with “Invoke-PnPSiteTemplate”.

      Reply
  • These are great, thanks for putting in the time to share!

    Reply
  • Hi,
    thanks for your work.

    Is it posible that I export sides by the last modification date?
    E.g.: I would like to export all sides modified in the time between 21.12. – 27.12.

    best regards
    Raphael

    Reply

Leave a Reply

Your email address will not be published.