Remove SharePoint WebParts from Page using Powershell

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )

{

Add-PSSnapin Microsoft.SharePoint.PowerShell

}

 
 

$siteUrl = “http://34.231.97.77

$spWeb = Get-SPWeb $siteUrl -ErrorAction Stop

 
 

#Declare the absolute path to the SharePoint page

$pagePath = “/Default.aspx”

$pageUrl = $siteUrl + $pagePath

write-host “Processing site: “, $siteUrl

write-host “Processing page: “, $pageUrl

 
 

#Initialise the Web part manager for the specified profile page.

$spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

 
 

#List all the Webparts in the specified page

foreach ($webpart in $spWebPartManager.WebParts)

{

write-host $siteUrl +”: Existing Web part – ” + $webpart.Title + ” : ” + $webpart.ID

break;

}

#Remove the Share Documents Web part from that page

foreach ($webpart in ($spWebPartManager.WebParts)) # # | Where-Object {$_.Title -eq “Shared Documents”}))

{

write-host $siteUrl +”: Existing Web part – ” + $webpart.Title + ” : ” + $webpart.ID

$webpart1 = $webpart

break;

}

 

#Delete the existing webpart

$spWebPartManager.DeleteWebPart($spWebPartManager.WebParts[$webpart1.ID])

write-host “Deleted the existing Shared Document web part.”

 
 

$spWeb.Dispose()

Leave a Reply

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

Bitnami