
Previously I have written a blog post on multiple ways to delete comments form a SharePoint page. There are scenarios to delete all comments from a page or few comments from the page. Or comments from specific user to be deleted from one page or multiple pages. This would not be simple as site owner or page owner can delete all comments or disable comments on the page.
I am sharing a PowerShell script which is used to delete comments from orphand users who are no longer part of the organization or you can list any specific users. I have used below PnP PowerShell module to remove the comments from all pages under the site pages library:
#connect to sharepoint online site
Connect-PnPOnline -Url "https://mstalk.sharepoint.com" -Interactive
#Name of list
$listName= "Site Pages"
$orphanedUserEmails = "orphanduser1@mstechtlak.com","orphanduser1@mstechtlak.com"
#Get all list items from list in batches
$listItems = Get-PnPListItem -List $listName -Fields "ID" -PageSize 500
foreach($item in $listItems)
{
try
{
$comments = Get-PnPListItemComment -List $listName -Identity $item.["ID"]
foreach($comment in $comments)
{
if($orphanedUserEmails -contains $comment.Author.LoginName.Replace("i:0#.f|membership|",""))
{
Remove-PnPListItemComment -List $listName -Identity $item["ID"] -Text $comment.Text -Force
}
}
}
catch
{
write-host -f Red "Error:" $_.Exception.Message
}
}
No Comments