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?
I’m trying to remove custom action banner, the script removes only for the top level, How can include subsites when removing custom action?
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”
}