SharePoint Online: Delete Alerts using PowerShell
Requirement: Delete Alerts in SharePoint Online.
How to Delete Alerts in SharePoint Online?
Alerts are an important part of SharePoint Online. While many of these alerts may be useful, there may also be alerts that you no longer want to receive. Thankfully, there is a way to delete SharePoint Online alerts. This blog post will walk you through the steps needed to delete alerts, and we will also provide a PowerShell to delete alerts to automate this process.
How to remove alert notifications in SharePoint Online? Well, To delete an alert from SharePoint Online, follow these steps:
- Login to SharePoint Online >> Navigate to the Site Settings page.
- Under the “Site Administration” section, click on the “User Alerts” link
- This page lists alerts of all users on the particular site (web). Pick the user from the drop-down and click on the “Update” button to retrieve all user alerts.
- Once alerts are listed, can select either a single alert or multiple alerts and then click on the “Delete Selected Alerts” link.
SharePoint Online: Delete All Alerts of a User using PowerShell
If you’re like me, you probably have a lot of alerts set up in SharePoint Online. And, if you’re like me, you may also find yourself occasionally wanting to delete all of them at once. Luckily, PowerShell makes this easy! I’ll show you how to delete all of your alerts using PowerShell.
Here is the PowerShell to delete all alerts of a particular user in the SharePoint Online site:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/projects"
$UserID="Salaudeen@crescent.com"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get All Alerts of the User
$User = $Ctx.Web.EnsureUser($UserID)
$UserAlerts = $User.Alerts
$Ctx.Load($User)
$Ctx.Load($UserAlerts)
$Ctx.ExecuteQuery()
#Loop through each alert
Write-host "Total Alerts Found:"$User.Alerts.Count
ForEach($Alert in $UserAlerts)
{
#Delete the Alert
Write-host -f Green "Alert '$($Alert.Title)' is Deleted on $($Alert.AlertType)"
$User.Alerts.DeleteAlert($Alert.ID)
$Ctx.ExecuteQuery()
}
}
Catch {
write-host -f Red "Error Deleting User Alerts!" $_.Exception.Message
}
Let’s wrap the above script into a re-usable function and remove all alerts of a user from a SharePoint Online site collection.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Remove User Alerts in a Site Collection
Function Remove-SPOUserAlerts($SiteURL, $UserID)
{
Try {
Write-host -f Yellow "Searching User Alerts in Site:"$SiteURL
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Web and its Subwebs
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.Load($Web.Webs)
$Ctx.ExecuteQuery()
#Get All Alerts of the User in the site
$User = $Web.EnsureUser($UserID)
$UserAlerts = $User.Alerts
$Ctx.Load($User)
$Ctx.Load($UserAlerts)
$Ctx.ExecuteQuery()
#Loop through each alert
Write-host "Total Number of User Alerts:"$User.Alerts.Count
ForEach($Alert in $UserAlerts)
{
#Delete the Alert
$User.Alerts.DeleteAlert($Alert.ID)
$Ctx.ExecuteQuery()
Write-host -f Green "Alert '$($Alert.Title)' is Deleted on $($Alert.AlertType)"
}
#Process Subsites
ForEach($Web in $Web.Webs)
{
#Call the function recursively
Remove-SPOUserAlerts -SiteURL $Web.Url -UserID $UserID
}
}
Catch {
write-host -f Red "Error Deleting User Alerts!" $_.Exception.Message
}
}
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$UserID="Salaudeen@crescent.com"
#Setup Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Call the function to remove alerts in site collection
Remove-SPOUserAlerts -SiteURL $SiteURL -UserID $UserID
Delete All User Alerts from the Web:
This PowerShell script deletes all alerts of all users in a SharePoint Online web.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/projects"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get All Alerts of the Web
$Web = $Ctx.Web
$WebAlerts = $Web.Alerts
$Ctx.Load($Web)
$Ctx.Load($WebAlerts)
$Ctx.ExecuteQuery()
#Loop through each alert
Write-host "Total Alerts Found:"$WebAlerts.Count
ForEach($Alert in $WebAlerts)
{
#Get the User of the Alert
$Ctx.Load($Alert.User)
$Ctx.ExecuteQuery()
$AlertUser = $Alert.user.Title
#Delete the Alert
$web.Alerts.DeleteAlert($Alert.ID)
$Ctx.ExecuteQuery()
Write-host -f Green "Alert '$($Alert.Title)' of '$($AlertUser)' is Deleted on $($Alert.AlertType)"
}
}
Catch {
write-host -f Red "Error Deleting Alerts!" $_.Exception.Message
}
PnP PowerShell to Remove User Alerts
Here is the PnP PowerShell script to remove user alerts in SharePoint Online using Get-PnPAlert and Remove-PnPAlert cmdlets:
$SiteURL ="https://crescent.sharepoint.com/sites/Retail"
$ListName = "Projects"
$UserID = "i:0#.f|membership|salaudeen@crescent.com"
#Connec tot SharePoint Online
Connect-PnPOnline -URL $SiteURL -Interactive
#Get All alerts of the user from a list
Get-PnPAlert -List $ListName -User $UserID
#Remove the alert
Remove-PnPAlert -Identity ab59b35f-7b9b-4189-8aed-18a9bf573adc -User $UserID -Force
If you want to disable alerts, instead of deleting them, use: SharePoint Online Disable Alerts using PowerShell