You can use the REST API to call the /Comments
endpoint:
/_api/web/lists/GetByTitle('Site Pages')/GetItemById(x)/Comments
Here is the PowerShell code:
$SiteURL = "https://yourtenant.sharepoint.com/sites/TargetSiteCollection"
Connect-PnPOnline $SiteURL -UseWebLogin
$ListName= "Site Pages"
$allSitePages = Get-PnPListItem -List $ListName
foreach ($sitePage in $allSitePages)
{
Write-host ------------------------------
$pageID = $sitePage["ID"]
Write-host 'Site Page' $pageID ':' $sitePage["Title"]
$RestMethodURL = $SiteURL+"/_api/web/lists/GetByTitle('$ListName')/GetItemById('$pageID')/Comments"
$ListOfComments = Invoke-PnPSPRestMethod -Url $RestMethodURL
if ($ListOfComments.value)
{
# Comments exsist!
Write-host $ListOfComments.value.Count comments found:
$ListOfComments.value
} else {
Write-host "No comments where found on this page."
}
Write-host ------------------------------ `n`n`n`n`n
}
Here is the PowerShell script output compared to the comments on the SharePoint page:
