Delete all Pages - Sharepoint Powershell

This post highlights how to remove all sub-sites or pages 
permanently from a specific SharePoint site collection/web using a PowerShell script. 

Prerequisite

  •    Before using the below script user needs admin-level access.
  •   Change the site collection name as per your requirements.

NOTE: Use Below Script and kindly pass the valid URLs/List Name to execute the script.

Script:

Add-PSSnapin Microsoft.SharePoint.PowerShell 

$site=Get-SPSite -identity <Your URL>
$subSiteColl=$site.RootWeb.Webs;
$subSites=@()
for($i= 0;$i -le $subSiteColl.Count-1;$i++)
{
   $subSites +=$subSiteColl[$i];
}

foreach($subSite in $subSites){  

    $targetWeb=get-spweb -identity $subSite.URL;
    $targetListGeneral=$targetWeb.lists["<List Name>"];     

    if($targetListGeneral){
         
        for($x=$targetListGeneral.ItemCount-1;$x -ge 0; $x--)
        {
    
            $item=$targetListGeneral.Items[$x];
    
            Write-Host "Deleting item id" $item.ID
            $item.Delete();
        }
        $targetListGeneral.Update();
     
    }
}

 Thank you for reading my post!

Comments

Popular posts from this blog

Power Apps June 2023 Updates (Canvas App Focused)

Effortlessly Import JSON Data into SharePoint with Power Automate Flow: A Step-by-Step Guide

Unleash the Power of Power Apps: Retrieve Over 2000 Records from a SharePoint List Effortlessly Without Delegates