1

I have a Site Pages library in Communication Site. I have to delete comments posted by 2 users on all the pages using PowerShell.

There are many solutions available which use REST but I prefer to use PowerShell.

1 Answer 1

0

I have used PnP PowerShell here,

$SiteURL="https://yoyo.sharepoint.com/sites/testsite"
$UserName="[email protected]"
$Password = "password"
$ListName = "Site Pages"
$deleteEmails = "[email protected]","[email protected]"
 
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword
 
Connect-PnPOnline -Url $SiteURL -Credentials $Cred

#Get all list items from list in batches
$ListItems = Get-PnPListItem -List $ListName -Fields "ID" -PageSize 500

Write-host "Total Items:" $($ListItems.Count)
 
ForEach($Item in $ListItems)
{ 
    Try
    {
        $Comments = Get-PnPListItemComment -List $ListName -Identity $Item["ID"]

        foreach($Comment in $Comments)
        {
            if($deleteEmails -contains $Comment.Author.LoginName.Replace("i:0#.f|membership|",""))
            {
                Write-Host "Id :" $Item["ID"]
                
                try
                {
                    Remove-PnPListItemComment -List $ListName -Identity $Item["ID"] -Text $Comment.Text -Force
                }
                Catch 
                {
                    Write-Host -f Red "Error while removing commnet:" $_.Exception.Message
                    Log-Message "Error while removing commnet:" $_.Exception.Message
                }
            }
        }
    }
    Catch 
    {
        Write-Host -f Red "Error while getting comments:" $_.Exception.Message
        Log-Message "Error while getting comments:" $_.Exception.Message
    }
}
1
  • What version of PnP.PowerShell are you using? I am getting The term 'Get-PnPListItemComment' is not recognized ... with version 1.10.0. May 30, 2022 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.