Get List of Modern Pages from SharePoint Online Sites


The script uses a list of all sites from SharePoint Online.  Check my earlier post Get all Sites and Subsites from SharePoint Online Sites to get the list of all sites.

$User = “admin@M365x086769.onmicrosoft.com”
#$User = Read-host “Please enter Office365 Admin User name username@domain.onmicrosoft.com. “
$File = “C:\365Creds\AllSites.csv”
$Creds = Get-Credentials

$SiteModernPages = @()
$CSVPath = “C:\temp\AllSites.csv”
$Data = Import-Csv -Path $CSVPath
if($Data)
{
     foreach($Site in $Data)
     {
         try
         {
             Connect-PnPOnline -Url $Site.SiteUrl -Credentials $Creds
             Write-Host “Working on $($Site.SiteUrl).”
             $items = $null
             $items = Get-PnPListItem -List “SitePages” -Fields ID,Title,BannerImageUrl,FileRef -ErrorAction SilentlyContinue
             if($items)
             {
                 foreach($ModernPage in $items)
                 {
                     Write-Host “.” -NoNewline
                     if($ModernPage[“BannerImageUrl”].Url -ne $null)
                     {
                         Write-Host “.” -NoNewline
                         $FileName = $ModernPage[“FileLeafRef”]
                         $PageUrl = “$($Site.SiteUrl)/SitePages/$FileName”
                         $PageObject = [PSCustomObject]@{Site = $Site.SiteUrl; PageTitle = $ModernPage[“Title”]; PageUrl = $PageUrl  }
                         $SiteModernPages +=$PageObject
                         Write-Host “Modern Page Found.  Added to List” -ForegroundColor Green
                     }
                 }
             }
        
             Disconnect-PnPOnline

        }
         catch
         {
             “Error was $_”
             $line = $_.InvocationInfo.ScriptLineNumber
             “Error was in Line $line”
         }

    }
}

#Export All Pages to CSV
$SiteModernPages | Export-Csv -Path C:\temp\SitesModernPages.csv -NoTypeInformation -Force
#Export Page Count by Site to CSV
$SiteModernPages | group Site | Select Name, Count | Export-Csv -Path C:\temp\SitesModernPagesCount.csv -NoTypeInformation –Force

Technet Link

https://gallery.technet.microsoft.com/Get-List-of-Modern-Pages-9ae35436