This script has some issue related to logging of failed site but site delete works fine.
# Completely deletes the specified Web (including all subsites)
function RemoveSPWebRecursively(
[Microsoft.SharePoint.SPWeb] $web)
{
Write-Debug "Removing site ($($web.Url))..."
$subwebs = $web.GetSubwebsForCurrentUser()
foreach($subweb in $subwebs)
{
RemoveSPWebRecursively($subweb)
$subweb.Dispose()
}
TRY
{
$DebugPreference = "SilentlyContinue"
Remove-SPWeb $web -Confirm:$false
$DebugPreference = "Continue"
}
Catch
{
[system.exception]
$text | Add-Content 'failedsites.txt'
}
}
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
Start-Transcript -path ".\SiteDelete.log"
$text | Set-Content 'failedsites.txt'
$csv = Import-Csv ".\delete.csv"
foreach ($line in $csv)
{
TRY
{
$web = Get-SPWeb $line.URL
If ($web -ne $null)
{
RemoveSPWebRecursively $web
$web.Dispose()
}
}
Catch
{
[system.exception]
$text | Add-Content 'failedsites.txt'
}
}
Stop-Transcript