SharePoint Online: Remove Web Part from Page using PowerShell

Requirement: Remove web part from a page in SharePoint Online using PowerShell

How to Delete a Web Part from a SharePoint Online Page?

Web parts are the widgets to show information, add business functionality, implement the process, act as an interface to databases or provide any other application-specific functionality. Web parts on a SharePoint Online page can be removed if they are no longer needed. You can remove a web part by:

  1. Navigate to the page where the web part is placed >> Click on Edit Page from settings menu.
  2. From the web part drop-down menu, select the Delete option and confirm the prompt.
  3. This removes the web part from the page.

PowerShell to Delete a web part from a Page in SharePoint Online:

We have a script editor web part, titled “Google Analytics Script” in SharePoint Online page and got a requirement to remove it using PowerShell.

#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"

#function to remove a web part from SharePoint Online page
Function Remove-SPOWebpartFromPage([String]$SiteURL,[String]$PageRelativeURL,[String]$WebPartTitle, [PSCredential]$Cred)
        #Setup Credentials to connect
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
        #Set up the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        #Get the web parts from the given web page
        $Web = $Ctx.web
        $Page= $Web.GetFileByServerRelativeUrl($PageRelativeURL)
        $WPM = $Page.GetLimitedWebPartManager("Shared")
        $WebParts = $WPM.WebParts
        write-host "Loaded web parts..."

        Write-host "Searching Web parts..."
        #Get the Web part ID to remove
        ForEach($WebPart in $WebParts)
            #Get Web part properties
            If($Webpart.WebPart.Properties.FieldValues["Title"] -eq $WebPartTitle)
                $WebPartID = $Webpart.ID

        If($WebPartID -ne [string]::Empty)
            write-host -f Green "Web Part '$($WebpartTitle)' has been removed!"
            write-host -f Yellow "Web Part '$($WebpartTitle)' not found!"
    Catch [System.Exception]
        Write-Host -f Red $_.Exception.Message

#Config parameters
$SiteURL = ""
$WebPartTitle="Google Analytics Script"

#Get credentials to connect to 
$Credentials = Get-Credential

#Call the function to remove the web part from page
Remove-SPOWebpartFromPage -SiteURL $SiteURL -PageRelativeURL $PageRelativeURL -WebPartTitle $WebpartTitle -Cred $Credentials

Delete Web Part using Web Part ID:
The above script deletes the web part from the page from the web part title. However, If you know the web part ID, you can remove the web part simply by:


Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply