Delete site collections with matching string

26/01/2013 00:51

param($matchingString, $matchingMonth)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ea 0

#USAGE: Call script without parameters to delete all site collections, with 1 or two parameters with matching string
#e. g.  .\DeleteSiteCollections.ps1 (to delete all sites)
#e. g.  .\DeleteSiteCollections.ps1 lukas (to delete all sites which contains 'lukas' in url)
#e. g.  .\DeleteSiteCollections.ps1 lukas 201212 (to delete all sites which contains 'lukas' and '201212' in url)
#$matchingString = "SomeStringInUrl"
#$matchingMonth = "201301"

$matchingSites = (get-spsite -limit all | where-object {($_.url -match $matchingString) -and ($_.url -match $matchingMonth)})
if($matchingSites -eq $null)
 Write-Host "No matching sites foud. Deletion will be aborted." -ForegroundColor Red
$allSites = Get-SPSite -Limit all

Write-Host ""
if($matchingSites.count -eq $allSites.count)
 Write-Host "ATTENTION: All sites were selected." -ForegroundColor Red
Write-Host ""
$answer=Read-Host "Do your really want to remove all sites from list above? [y/n]"
if($answer -ne "y")
 Write-Host "Deletion aborted."
 foreach($site in $matchingSites)

  write-host -ForegroundColor Green "deleting site " $siteUrl "..."
  Remove-SPSite -Identity $siteUrl -Confirm:$false -GradualDelete