The eGroup application services and development is currently working on a complex SharePoint 2010 deployment that involves Nintex workflow creation of sites using site templates and web services. While preparing for a demonstration we decided to remove all test sites that had been created up until that point and there were more than 50!.I'm certain you can all imagine the realization of how long this would take when faced with 5 minutes before demo time! Entering each site, visiting site settings, choosing to delete the site, confirming, rinse and repeat would have take 30+ minutes. A quick script solved the problem for us - read more to see this simple and effective script.

<!--more-->

We needed to deploy RegularExpressions in our solution as other sites are housed in this same collection - such as our all important Site Template that has many hours of customizations and is used to spin up the individual sites that we were attempting to delete. The solution is below.

<em>Note the -WhatIf option in the Remove-SPWeb command. This will prevent the action from actually being executed and will instead report what would happen it this command were allowed to run. Please test before removing this option! Also, the -Confirm:$false option will suppress all "are you sure" prompts and will silently perform the request commands. Please remove this command after removing the -WhatIf option to be sure it is going to delete the desired sites. It also would not hurt to break out the RegEx commands and test them independently before executing this command. Remove-SPWeb could be replaced by Write-Output $_.Name.</em>

<em>As with any destructive commands take a good backup before running the script!</em>

Get-SPSite https://RootSite/WhereSitesExist/ | Get-SPWeb -Limit ALL | ForEach-Object{
if ([regex]::IsMatch($_.Name,"RegExToMatchSiteName"))
{
Remove-SPWeb -WhatIf -Identity $_.Url -Confirm:$false
}
}