Remove modern web parts from pages
Summary
Script will remove web part(s) on multiple pages by their Id, Instance Id, or Title
Implementation
- Open Windows PowerShell ISE
- Edit Script and add details like SharePoint tenant URL, Term groups, and the output directory
- Press run
Warning
Please be aware this script contains a command that will remove or delete an artifact, ensure you test and understand the implications of running the script.
Function Remove-WebpartFromPages() {
PARAM (
[Parameter(Mandatory = $true)]
[string]$SiteURL,
[Parameter(Mandatory = $false)]
[string[]]$WebPartIds,
[Parameter(Mandatory = $false)]
[string]$ContentTypeId,
[Parameter(Mandatory = $false)]
[string[]]$Pages
)
Try {
## Connect to SharePoint Online site
Write-Host "Connect to $($SiteURL)"
Connect-PnPOnline -URL $SiteURL -UseWebLogin
$pageItems = @()
$skippedPages = @()
# If page parameter is empty, loop through all pages
if ($Pages.Length -lt 1) {
$pageItems = Get-PnPListItem -List "Site Pages"
$Pages = $pageItems | ForEach-Object { $_["FileLeafRef"] }
}
if($Pages.Length -lt 1){
$pageItems = Get-PnPListItem -List "Site Pages"
}
if ($ContentTypeId) {
$pageItems = $pageItems | Where-Object {$_["ContentTypeId"].toString() -eq $ContentTypeId}
}
if($pageItems.Length -ge 1){
$Pages = $pageItems | ForEach-Object{ $_["FileLeafRef"] }
}
$Pages | ForEach-Object {
$fileLeafRef = $_
Write-Host "Processing $fileLeafRef"
try {
$page = Get-PnPPage -Identity $fileLeafRef
if($WebPartIds.Length -ge 1){
$controls = $page.Controls | Where-Object {$WebPartIds -contains $_.Title -or $WebPartIds -contains $_.WebPartId}
}
$controls | ForEach-Object {
Write-Host "Removing web part: $($_.Title)"
Remove-PnPPageComponent -Page $page -InstanceId $_.InstanceId -Force
Write-Host "Web part $($_.Title) removed successfully from $($fileLeafRef)" -ForegroundColor "green"
}
}
catch {
$skippedPages += $fileLeafRef
Write-Host "Skipped $fileLeafRef" -ForegroundColor "yellow"
}
}
}
Catch {
Write-Host "Error: $($_.Exception)" -ForegroundColor Red
Break
}
## Disconnect the context
Disconnect-PnPOnline
}
Remove-WebPartFromPages -SiteURL https://contoso.sharepoint.com -WebPartIds "News","0ec51ebc-4754-4ef4-a953-1a3adb4b4d8c" -Pages "home.aspx","pnp.aspx"
# More examples
<#
# Specific content type
Remove-WebPartFromPages -SiteURL https://contoso.sharepoint.com -WebPartIds "News" -ContentTypeId "0x0101009D1CB255DA764"
#>
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
Ramin Ahmadi |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.