Advertisement

Responsive Advertisement

Programmatically Delete a SharePoint Folder from each site using Powershell

In this post I’ll show you how to write a Pow­er­Shell script that will delete a sin­gle folder from mul­ti­ple sites. In this case, there’s a folder called “Shared With Everyone” in the Site Pages doc­u­ment library, as shown here:

This folder is gen­er­ated by My Site Creation for every user, which always cre­ates the same folder name in the “Site Pages” Doc­u­ment Library of any site or sub-site. I’d like to sim­ply delete it.

$url = "http://sharepoint"
$folder = "Shared With Everyone"
 
$sites = Get-SPSite -WebApplication $url | Where-Object {$_.Url -like "$url/sites/*"}
foreach ($site in $sites)
 {
 $siteid = $site | Select-Object -ExpandProperty Url
 $webs = Get-SPWeb -Site $siteid -Limit ALL
 foreach ($web in $webs)
  {
  $library = $web.Folders["SitePages"]
  $allcontainers = $library.SubFolders | select Name
  Foreach ($container in $allcontainers)
   {
    If ($container -match $folder)
     {
     $library.SubFolders.Delete($folder)
     Write-Host "Deleted `"$folder`" from $web" -foregroundcolor Red
     }
    else {Write-Host "Finished checking $web." -foregroundcolor DarkGreen}
   }
  }
 }
Write-Host "Finished checking all sites."

Post a Comment

0 Comments