SharePoint Online: Remove Custom Action Using PowerShell CSOM

Requirement: Delete a Custom Action in SharePoint Online using PowerShell.

SharePoint Online: PowerShell to Remove Custom Action

Here is the PowerShell CSOM script to remove custom action:

#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"
  
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$CustomActionTitle = "SharePoint Admin Center"
 
Try{
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get All Custom Actions
    $Web = $Ctx.Web
    $UserCustomActions= $Web.UserCustomActions
    $Ctx.Load($UserCustomActions)
    $Ctx.ExecuteQuery()

    #Get Custom Action(s) to delete
    $CustomActions = $UserCustomActions | Where { $_.Title -eq $CustomActionTitle }

    If($CustomActions -ne $Null)
    {
        #Delete all custom actions
        $CustomActions | ForEach-Object {
            #Remove the custom action
            $_.DeleteObject()
            Write-Host -f Green "Custom Action '$($_.Name)' Deleted Successfully!"
        }
        $Ctx.ExecuteQuery()
    }
    Else
    {
         write-host -f Yellow "Custom Action Does not Exist!"
    }
}
Catch {
        write-host -f Red "Error Removing Custom Action!" $_.Exception.Message
}

This deletes all custom actions with the provided title. You can get and delete them using their name, ID, or other parameters as well.

PnP PowerShell to Delete a Custom Action in SharePoint Online

Use the Remove-PnPCustomAction PnP cmdlet to remove a custom action in SharePoint Online.

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"

#Connect to SharePoint Online Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get Custom Actions
Get-PnPCustomAction

#Remove Custom Action
Remove-PnPCustomAction -Identity "SharePoint Admin Center" -Force

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *