How can i delete with powershell all sites from a sitecollection without the rootweb?

I found this:

Get-SPSite <SiteUrl> | Get-SPWeb -Limit All | ForEach-Object {Remove-SPWeb -Identity $_ -Confirm:$false}

but it iterates over all webs, which means it will iterate over the root web and I dont want to delete the rootweb


2 Answers 2


Try this one, changing the url to the url of your Rootweb

$url = "http://portal.com"
$subsites = ((Get-SPWeb $url).Site).allwebs | ?{$_.url -like $url +"/*"}

foreach($subsite in $subsites) { Remove-SPWeb $subsite.url }
($s = Get-SPSite <SiteUrl>) | Get-SPWeb -Limit All | Where-Object {$_.Id -ne $s.RootWeb.Id} | ForEach-Object {Remove-SPWeb -Identity $_ -Confirm:$false}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.