Introduction

Classic SharePoint sites have classic pages like wiki pages, web part pages, blog pages or publishing pages which cannot be used in modern user interfaces. But a classic site can host modern pages which enables a great end user experience.
In this blog we are going to discuss about how to transform classic pages to modern page using PNP PowerShell.

Powershell code

In this script, we have elaborated the steps for converting a wiki page or a webpart page to Modern page,
  1. First give the site url and then set the credentials.
  2. Get all the pages from Site page library. We have used '-PageSize' parameter to ensure the query works when there are more than 5000 items in the list.
  3. We have used 'ConvertTo-PnPClientSidePage' for converting the classic page to modern page. I have elaborated the properties which we used for modernization.

TakeSourcePageName

If we set the 'sourcePageName' to $false , It will create a new modern page with name'Migrated_<pagename>.aspx'. If we set it to $true, It will rename the old classic page name as 'Previous_<pagename>.aspx' and the new modern page will be created as '<pagename>.aspx'.
Overwrite
If you run the modernization multiple times you need to overwrite the target page.
LogVerbose
If you want more details logged add this switch to enable verbose logging
KeepPageCreationModificationInformation
It keeps the same author/editor/created/modified information in the modern page as the original page.
CopyPageMetadata
It use to copy the metadata of the original page to creat modern page.
Code snippet
  1. try {
  2. $SiteUrl = Read - Host "SiteUrl"
  3. $sourcePageName = $false
  4. $folderLocation = $(Get - Location)
  5. Connect - PnPOnline - Url $SiteUrl - UseWebLogin
  6. sleep 5
  7. try {
  8. Write - Host "Enabling the modern page feature." - ForegroundColor Green
  9. Enable - PnPFeature - Identity "B6917CB1-93A0-4B97-A84D-7CF49975D4EC" - Scope Web - Force
  10. } catch {}
  11. $pages = Get - PnPListItem - List sitepages - PageSize 500
  12. Write - Host "Starting the modernization." - ForegroundColor Green
  13. Foreach($page in $pages) {
  14. $pageName = $page.FieldValues["FileLeafRef"]
  15. if ($page.FieldValues["ClientSideApplicationId"] - eq "b6917cb1-93a0-4b97-a84d-7cf49975d4ec") {
  16. Write - Host ""
  17. $page.FieldValues["FileLeafRef"]
  18. " is a modern page" - ForegroundColor Yellow
  19. } else {
  20. Write - Host "Converting $($pageName) to modern page" - ForegroundColor Cyan
  21. ConvertTo - PnPClientSidePage - Identity $page.FieldValues["ID"] `
  22. -Overwrite ` - TakeSourcePageName: $sourcePageName `
  23. -LogType File ` - LogFolder $folderLocation `
  24. -LogSkipFlush ` - KeepPageCreationModificationInformation `
  25. -CopyPageMetadata
  26. }
  27. }
  28. Write-Host "The conversion log file." -ForegroundColor Green
  29. Save-PnPClientSidePageConversionLog
  30. Write-Host "Successfully converted to modern pages." -ForegroundColor Green
  31. Disconnect-PnPOnline
  32. }
  33. catch {
  34. Write-Host -ForegroundColor Red 'Error ', ':' $Error[0].ToString();
  35. sleep 10
  36. }
Classic site page
Convert Classic Pages To Modern Pages Using PnP PowerShell Script
Run the script, give the SiteUrl.
Convert Classic Pages To Modern Pages Using PnP PowerShell Script
Give the credentials.
Convert Classic Pages To Modern Pages Using PnP PowerShell Script
While running the script it will show message as below.
Convert Classic Pages To Modern Pages Using PnP PowerShell Script
Convert Classic Pages To Modern Pages Using PnP PowerShell Script
After successfully running the script the modern pages will be like the below screenshots.
Convert Classic Pages To Modern Pages Using PnP PowerShell Script
Classic page
Convert Classic Pages To Modern Pages Using PnP PowerShell Script
Modern page
Convert Classic Pages To Modern Pages Using PnP PowerShell Script