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 = “”
#$User = Read-host “Please enter Office365 Admin User name “
$File = “C:\365Creds\AllSites.csv”
$Creds = Get-Credentials

$SiteModernPages = @()
$CSVPath = “C:\temp\AllSites.csv”
$Data = Import-Csv -Path $CSVPath
     foreach($Site in $Data)
             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
                 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

             “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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s