Converting Site Collections to SubWebs for MOSS 2007 using powershell

We have a SharePoint farm where each site was created as a site collection, instead of a SharePoint Subsite. Each site collection needs to be move to site under one site collection in a bunch of subwebs. As there was 100 odd sites to copy, it made sense to script using powershell.

Unlike in SharePoint 2010, powershell has cmdlets that can easily talk to SharePoint sites. In 2007, you can still talk to SharePoint, but not through using the cmdlets. You need to write code similar to if it was a console application in C#.

In the code below, our enviroment is set up so that all our Site Collections sites sit under http://content/sites/. We have created a new database and a single site collection so that all the site collections will site under one site collection found at http://content/archive/london/

The code below will show you how I converted the numerous Site Collections to be one site collection with numerous webs.

##Reference to SharePoint DLL
## Probable location of sharepoint STSADM utility program
$stsadm = "c:\program files\Common Files\Microsoft Shared\Web Server Extension\12\bin\stsadm.exe"

$startsite = "http://Content/"

$EndSiteCollectionURL = "http://Content/Archive/London"
$SiteCollectionTemplate = "STS#0"
$SiteCollectionLanguage = 1033

$tempRoot = New-Object Microsoft.SharePoint.SPSite($startsite);
$tempRootCol = $tempRoot.WebApplication.Sites;
$endRoot = New-Object Microsoft.SharePoint.SPSite($EndSiteCollectionURL)
$webRoot = $endRoot.OpenWeb()
foreach($webApp in $tempRootCol)
if($webApp.Url -like "*/sites/*")
   #Get the URL Only

   $relativeURL = $webApp.ServerRelativeUrl;

   $trimVal = $relativeURL.LastIndexOf('/');

   $url = $relativeURL.Substring($trimVal + 1)</span>

$currentWebApp = $webApp.OpenWeb();
$currentTitle = $currentWebApp.Title;
$currentDescription = $currentWebApp.Description;
$fileName = "d:\CMPFiles\" + $url + ".cmp"

   $fileNameWildCardDelete = "d:\CMPFiles\" + $url + "*.cmp*"</span>

#Export Content
Write-Host Backing up $currentTitle
&stsadm -o Export -url $webApp.Url -filename $fileName -includeusersecurity

#Create subsite
Write-Host Creating Site for $currentTitle
 $webSiteCreated = $webRoot.Webs.Add($url, $currentTitle, $currentDescription, $SitecollectionLanguage, $SiteCollectionTemplate, $false, $false);
Write-Host Site Created for $webSiteCreated.Url

#Deleting Standard lists and documents.
$webSiteCreated.Lists["Shared Documents"].Delete();
$WebSiteCreated.Lists["Team Discussion"].Delete();

Write-Host Restoring $currentTitle to $webSiteCreated.Url
&stsadm -o Import -url $webSiteCreated.Url -filename $fileName -includeusersecurity
Write-Host Restored $currentTitle to $webSiteCreated.Url

#Remove-Item $FileNameWildCardDelete


Now I’m not saying this is perfect. It does copy everything from a Site Collection to a collection of webs, however, watching the logs that are displaying on my screen, some lookups lists are not found.

As we just want the Site Collections converted to Webs, so that we can put all in one database to then upgrade to SharePoint 2010 as an archive site, it meets our needs.

If any new updates to this code is made, I will update this blog.