Remove a Web Part from All Pages in SharePoint Site Collection using PowerShell

Requirement: After the SharePoint migration from SharePoint 2013 to SharePoint 2016, we decided to remove a broken web part from all pages wherever it’s being used.

How to Remove a Web Part from Page in SharePoint?

To remove a web part from the page, you can edit the page and simply delete the web part. In case of page crash, use web part maintenance mode by appending “?contents=1” to the URL and then removing the unwanted web part. E.g., https://intranet.crescent.com/SitePages/Home.aspx?contents=1

sharepoint 2013 remove web part from page

PowerShell to Remove a Web Part from All Pages in a Site Collection:

Instead of deleting a web part from every page from the browser, we can leverage PowerShell to remove a particular web part from all pages in a site collection.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration parameters
$SiteURL = "https://intranet.crescent.com"
$WebPartTypeName="*BambooCalendar*"

#Get All Subsites in a site collection and iterate through each
$Site = Get-SPSite $SiteURL
ForEach($Web in $Site.AllWebs)
{

    Write-host Processing $Web.URL
    # If the Current Web is Publishing Web
    if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))
    {
        #Get the Publishing Web 
        $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
                   
        #Get the Pages Library
        $PagesLib = $PubWeb.PagesList  #Pages Library
     }
     else
     {
        $PagesLib = $Web.Lists["Site Pages"]
     } 

        #Iterate through all Pages  
        foreach ($Page in $PagesLib.Items | Where-Object {$_.Name -match ".aspx"}) 
        {
            $WebPartsToDelete = @()
            $PageURL=$web.Url+"/"+$Page.File.URL
            write-host $pageurl
            $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
                 
            #Get All Web Parts of given type
            foreach ($WebPart in $WebPartManager.WebParts | Where-Object { $_.GetType().ToString() -like $WebPartTypeName} )
            {
                write-host "Found the Web Part at "$PageURL -f Green
                $WebPartsToDelete += $WebPart.ID
            }
            #If the web part to delete is found
            If($WebPartsToDelete)
            {
                #Checkout if required
                if ($Page.File.RequiresCheckout) 
                { 
                    if ($Page.File.CheckOutStatus -ne "None") 
                    {  
                        write-host "Overriding Checkout..."
                        $Page.File.UndoCheckOut()
                    }
                    write-host "Checking Out..."
                    $Page.File.CheckOut()
                }

                #Remove the web part from page          
                foreach ($WebPart in $WebPartsToDelete)
                {
                   Write-Host "Deleting Web Part on $($web.Url)/$($page.Url)"
                   $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)       
                   $WebPartManager.DeleteWebPart($WebPartManager.webParts[$webPart])
                }
                
                if ($Page.File.CheckOutStatus -ne "None") 
                { 
                    Write-host "CheckIn-In..."
                    $Page.File.CheckIn("Deleted web part")
                    $Page.File.Publish('')
                }

                if ($Page.ParentList.EnableModeration) 
                { 
                    write-host "Approving..."
                    $Page.File.Approve('') 
                }
            }
        }
}

This script iterates through all sites and pages in the entire site collection and removes the given web part.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

Leave a Reply

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