On Modern SharePoint pages content creators can turn on or off comments on content pages as needed. By default comments are always turned on.
Script to Disable Page Comments on SharePoint Online
However, in some instances, you may want to disable Page Comments on the specific site for already created pages rather than going individually through each page or disabling comments on a tenant level.
You can achieve this by running the below script in SharePoint Online Management Shell:
Note, you will need to update $SiteURL line by entering the URL of the site you want to disable comments on.
#Parameter
$SiteURL= " <yourtenantURL/sites/sitename>"
$LibraryName = "SitePages"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get all Pages from the Library
$Pages = Get-PnPListItem -List $LibraryName
#Loop through each Page
ForEach($Page in $Pages)
{
#Disable Comments
Write-host "Disabling Comments on page:"$Page["FileLeafRef"]
Set-PnPClientSidePage -Identity $Page["FileLeafRef"] -CommentsEnabled:$False
}
Once you run the command you will be prompted to enter your credentials – enter the email address and password of the service account you’re using:
If you navigate to Site Pages (cog -> Site Contents -> Site Pages) area of your site, you will notice page Modified Date is changing as the command iterates through all site pages:
When you open one of the updated pages you won’t see page comments whereas other socials (likes and views) are displayed on the page.