Delete Sites with PowerShell
Published Feb 08 2019 12:49 PM 314 Views
Microsoft

First published on MSDN on Oct 04, 2012

A customer recently needed some help deleting a bunch of sites of old sites in an environment that was no longer in use. They used STSADM enumsites to take an inventory of what was in the farm/web application. They used the greater than(>) symbol in the command line to send the output to a file. Enumsites happens to dump out XML. Once they did that, they had someone review the sites, and remove the sites that were still needed, creating a list/file of sites to delete. They needed a way to call STSADM deletesites, using an input file. This can be done with PowerShell. Here's an example.


[xml]$SitesToDelete = Get-Content C:\folder\SitesToDelete.xml
$SitesToDelete.Sites.Site | % { & "$env:ProgramFiles\common files\microsoft shared\web server extensions\12\bin\stsadm.exe" -o deletesite -url $($_.url) -gradualdelete }

We can see here that we set a variable $SitesToDelete as the content of our XML file. Then we read the XML, for each site entry by appending on Sites.Site. From there, we call the STSADM command, adding in the URL of the site to delete using $_.url, which reads the URL value from the site entry in our XML.

 

You can use this type of syntax/logic to call a number of different STSADM commands with PowerShell. Make sure you adjust the path properly for SharePoint 2007(12), SharePoint 2010(14) and SharePoint 2013(15).

Version history
Last update:
‎Apr 28 2020 12:13 PM
Updated by: