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