In this post I’ll show you how to write a PowerShell script that will delete a single folder from multiple sites. In this case, there’s a folder called “Shared With Everyone” in the Site Pages document library, as shown here:
This folder is generated by My Site Creation for every user, which always creates the same folder name in the “Site Pages” Document Library of any site or sub-site. I’d like to simply delete it.
This folder is generated by My Site Creation for every user, which always creates the same folder name in the “Site Pages” Document Library of any site or sub-site. I’d like to simply 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."