SharePoint Online: Create Alerts using PowerShell

Requirement: Create SharePoint Alert using PowerShell

How to Create Alerts in SharePoint Online?

Alerts in SharePoint Online help to track changes such as addition/changes/deletes to the content, E.g., Lists and libraries, pages, etc. You can specify the conditions under which you want to receive alerts and the frequency with which you want to receive them. To create an Alert for a SharePoint Online document library, follow these steps:

  • Go to your SharePoint Online site, Navigate to the document library for which you want to create an alert
  • From the Toolbar, click on the ellipses and select “Alert Me”.
    sharepoint online powershell create alert
  • In the new alert creation page, type the alert title, enter users who will receive notifications,  Select E-mail or SMS for “Delivery Method”. Select the condition for this alert (e.g. “Anything changes”) and when to send alerts.
    create sharepoint online alert powershell
  • Scroll to the bottom of the page and click “OK” to create an alert for the document library.

Now, let’s create a SharePoint Online alert using PowerShell!

SharePoint Online: CSOM PowerShell to Create Alert

We want to add an alert programmatically using PowerShell to a SharePoint Online list when new items are added.

#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"
$ListName="Documents"
$UserID="[email protected]"

#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 the List and User Objects
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $User = $Ctx.Web.EnsureUser($UserID)
    $Ctx.Load($List)
    $Ctx.Load($User)
    $Ctx.ExecuteQuery()

    #Create new alert Object and Set its Properties
    $Alert = New-Object Microsoft.SharePoint.Client.AlertCreationInformation
    $Alert.List = $List
    $Alert.User = $User
    $Alert.Title = "New Documents in Project Docs - Alert"
    $Alert.AlertFrequency = [Microsoft.SharePoint.Client.AlertFrequency]::Immediate
    $Alert.AlertType = [Microsoft.SharePoint.Client.AlertType]::List
    $Alert.DeliveryChannels = [Microsoft.SharePoint.Client.AlertDeliveryChannel]::Email
    $Alert.Status = [Microsoft.SharePoint.Client.AlertStatus]::On
    $Alert.EventType = [Microsoft.SharePoint.Client.AlertEventType]::AddObject #Or All
    $Alert.Filter = "0" #Anything Changes - Other values: 1, 2, 3

    # Add the alert for the user
    $AlertGuid = $User.Alerts.Add($Alert)
    $User.Update()
    $Ctx.ExecuteQuery()

    Write-host -f Green "New Alert Has been Created!"
}
Catch {
    write-host -f Red "Error Creating Alert!" $_.Exception.Message
}

PnP PowerShell to Create New Alert in SharePoint Online

If you are looking for a way to create new alerts in SharePoint Online without having to use the UI, PowerShell is the answer! Let me show you how to use PnP PowerShell to create new alerts in SharePoint Online.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/CorporateBranding"
$UserId= "i:0#.f|membership|[email protected]"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
 
    #Create an Alert
    Add-PnPAlert -Title "Documents Alert" -List "Documents" -User $UserId -Frequency Daily -ChangeType All
      
    Write-host "New Alert has been Created for the given user!" -f Green    
}
Catch {
    Write-host -f Red "Error:" $_.Exception.Message
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

3 thoughts on “SharePoint Online: Create Alerts using PowerShell

  • Hi Thanks, for sharing this script, i have observed in this script $Alert.Filter Property values are not updated in target. Could you share who we can update the $Alert.Filter Property value.

    Reply
  • hi the script is very good but iam unable to update the $Alert.Filter value

    Reply
    • yes iam also facing the same problem please let me know if anyone got how to update that feature

      Reply

Leave a Reply