How to Remove Custom Action in SharePoint using PowerShell?

Requirement:  Remove a Custom Action in SharePoint using PowerShell.

Delete Custom Action using PowerShell in SharePoint

Here is how to remove custom action in SharePoint 2013 using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Site variable
$SiteURL="https://intranet.crescent.com"
$CustomActionTitle ="Support Center"

Try {
    #Get the Web
    $Web = Get-SPWeb $SiteURL
 
    #Get the Custom Actions Filter by Title
    $CustomActions = $web.UserCustomActions | Where { $_.Title -eq $CustomActionTitle } | Select ID, Title

    If($CustomActions -ne $Null)
    {
        #Delete custom action(s)
        $CustomActions | ForEach-Object {
            #Remove the custom action
            $web.UserCustomActions.Item($_.Id).Delete()
            Write-Host -f Green "Custom Action '$($_.Title)' Deleted Successfully!"
        }
    }
    Else
    {
        write-host -f Yellow "Custom Action '$CustomActionTitle' Doesn't Exist!"
    } 
} Catch {
    Write-Host -ForegroundColor Red "Error:" $_.Exception.Message
}

To delete all custom actions from the given scope, simply remove the “Where” class from the script!

Let’s make it a re-usable function:

Function Remove-CustomAction
{
    param 
    (        
[parameter(Mandatory=$true, ParameterSetName='Site')]
[Microsoft.SharePoint.SPSite]$Site,
[parameter(Mandatory=$true, ParameterSetName='Web')]
[Microsoft.SharePoint.SPWeb]$Web,
[parameter(Mandatory=$true, ParameterSetName='Site')] 
[parameter(Mandatory=$true, ParameterSetName='Web')]
[string]$ActionName
    )
 
    begin
    {
        $existingActions = @()
    }
    process
    {
        if( $PSCmdlet.ParameterSetName -eq "Site" )
        {
            $Site.UserCustomActions | ? { $_.Title -eq $ActionName } | % { $existingActions += $_ }
            $existingActions | % { $Site.UserCustomActions.Item($_.Id).Delete() }
        }
        else
        {
            $Web.UserCustomActions | ? { $_.Title -eq $ActionName } | % { $existingActions += $_ }
            $existingActions
            $existingActions | % { $Web.UserCustomActions.Item($_.Id).Delete() }
        }
    }
    end
    {
    }
}
#Call the function
Remove-CustomAction -Site $site -ActionName "SiteBanner"

My related post on deleting custom action in SharePoint Online: How to Delete a Custom Action in SharePoint Online using PowerShell?

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!

2 thoughts on “How to Remove Custom Action in SharePoint using PowerShell?

  • I’m trying to remove custom action banner, the script removes only for the top level, How can include subsites when removing custom action?

    Reply
    • Just, loop through the subsites:

      #Get the Site Collection
      $Site = Get-SPSite $SiteURL

      #Loop through each web of the site collection
      ForEach($Web in $Site.AllWebs)
      {
      #Call the function to remove custom action
      Remove-CustomAction -Web $Web -ActionName “SiteBanner”
      }

      Reply

Leave a Reply

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