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.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityI 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
}
}
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