#----------------------------------------------------------------------------------------------------------------------- # Name : ConvertSubSiteToSiteCollection.ps1 # # Purpose : # This script will take a SharePoint sub-site and move it into a site collection. # Used for SharePoint 2010 & SharePoint 2013. # # Prerequisite # The managed path that is to be used should already be created on the SharePoint farm. # # Author : Ian Ballard # # Version number : 1.00 # # Version date : 22 October 2014 # # History # Version number | Data | Comment # 1.00 | 22 October 2014 | Initial script. #----------------------------------------------------------------------------------------------------------------------- # #Script Options # $debug = $true #Write the commands out to the screen, , handy if you want to create a script to run later. #$debug = $false #Do not write the commands out to the screen. #$execute = $true #Run the commands $execute = $false #Do not run the commands, handy if you want to create a script to run later. $MessageTextColour = "red" $MessageCommandColour = "green" # #Sub site information. # $SubWeb = "http:///" # #Backup location formation. # $BackUpLocation = "\\\Backup" # #New site collection information # $WebApplication = "http://" $SiteCollectionURL = "http:////" $SiteCollectionName = "" $ContentDatabasePrefix = "" $SiteCollectionOwner = "domain\user" # # # #----------------------------------------------------------------------------------------------------------------------- #Do not edit below this line. #----------------------------------------------------------------------------------------------------------------------- # # # if ( $execute -eq $true ) { $startTime = Get-Date Write-Host "Start time : $startTime" -ForegroundColor $MessageTextColour } #----------------------------------------------------------------------------------------------------------------------- #Include the SharePoint PowerShell snap-in. #----------------------------------------------------------------------------------------------------------------------- if ( $debug -eq $true ) { Write-Host "Adding the SharePoint PowerShell snap-in" -ForegroundColor $MessageTextColour write-host "Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue" -ForegroundColor $MessageCommandColour } if ( $execute -eq $true ) { Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue } #----------------------------------------------------------------------------------------------------------------------- #Go and get the site template form the $SubWeb #----------------------------------------------------------------------------------------------------------------------- $web = Get-SPWeb -Identity $SubWeb $Template = $web.WebTemplate $Template = $Template + "#" $SiteTemplate = $Template + $web.WebTemplateID $SubWebCompatibilityLevel = 15 #Default is 15. switch( $web.UIVersion ) { 4 { #SharePoint 2010 or 2013 running in 14 mode $SubWebCompatibilityLevel = 14 } 15 { #SharePoint 2013 $SubWebCompatibilityLevel = 15 } default { throw "This script is only designed to support UIVersion 14 (SharePoint 2010) and 15 (SharePoint 2013), sorry, feel free to amend the script to include other versions but I can not guarantee it will work." } } #----------------------------------------------------------------------------------------------------------------------- #Export the sub web. #----------------------------------------------------------------------------------------------------------------------- $ExportLocation = $BackUpLocation + "\" + $SiteCollectionName + ".cmp" if ( $debug -eq $true ) { Write-Host "#Exporting the site with the following command." -ForegroundColor $MessageTextColour write-host "Export-SPWeb -Identity $SubWeb -Path $ExportLocation -IncludeUserSecurity -IncludeVersions all" -ForegroundColor $MessageCommandColour } if ( $execute -eq $true ) { Export-SPWeb -Identity $SubWeb -Path $ExportLocation -IncludeUserSecurity -IncludeVersions all } #----------------------------------------------------------------------------------------------------------------------- #Create the new content database. #----------------------------------------------------------------------------------------------------------------------- $ContentDatabaseFullName = $ContentDatabasePrefix + "_" + $SiteCollectionName if ( $debug -eq $true ) { Write-Host "#Creating the content database ($ContentDatabaseFullName) within the web application ($WebApplication)." -ForegroundColor $MessageTextColour write-host "New-SPContentDatabase -Name $ContentDatabaseFullName -WebApplication $WebApplication" -ForegroundColor $MessageCommandColour } if ( $execute -eq $true ) { New-SPContentDatabase -Name $ContentDatabaseFullName -WebApplication $WebApplication } #----------------------------------------------------------------------------------------------------------------------- #Create the new site collection in the new content database. #----------------------------------------------------------------------------------------------------------------------- if ( $debug -eq $true ) { Write-Host "#Create the new site collection in the new content database." -ForegroundColor $MessageTextColour write-host "New-SPSite -Url $SiteCollectionURL -OwnerAlias $SiteCollectionOwner -Template $SiteTemplate -ContentDatabase $ContentDatabaseFullName -Name $SiteCollectionName -CompatibilityLevel $SubWebCompatibilityLevel" -ForegroundColor $MessageCommandColour } if ( $execute -eq $true ) { New-SPSite -Url $SiteCollectionURL -OwnerAlias "$SiteCollectionOwner" -Template "$SiteTemplate" -ContentDatabase "$ContentDatabaseFullName" -Name "$SiteCollectionName" -CompatibilityLevel $SubWebCompatibilityLevel } #----------------------------------------------------------------------------------------------------------------------- #Import the backup. #----------------------------------------------------------------------------------------------------------------------- if ( $debug -eq $true ) { Write-Host "#Importing the site with the following command." -ForegroundColor $MessageTextColour write-host "Import-SPWeb $SiteCollectionURL -Path $ExportLocation" -ForegroundColor $MessageCommandColour } if ( $execute -eq $true ) { Import-SPWeb $SiteCollectionURL -Path $ExportLocation } #----------------------------------------------------------------------------------------------------------------------- #Lock down the content database so no other site collections are created in it. #----------------------------------------------------------------------------------------------------------------------- if ( $debug -eq $true ) { Write-Host "#Locking the content database ($ContentDatabaseFullName) down so no other site collections are created in it." -ForegroundColor $MessageTextColour write-host "Get-SPContentDatabase -Identity $ContentDatabaseFullName | Set-SPContnetDatabase -MaxSiteCount 1 -WarningSiteCount 0 " -ForegroundColor $MessageCommandColour } if ( $execute -eq $true ) { Get-SPContentDatabase -Identity $ContentDatabaseFullName | Set-SPContentDatabase -MaxSiteCount 1 -WarningSiteCount 0 } #----------------------------------------------------------------------------------------------------------------------- # Write out the script run time. #----------------------------------------------------------------------------------------------------------------------- if ( $execute -eq $true ) { $endTime = Get-Date Write-Host "Script execution time : $( New-TimeSpan $startTime $endTime)." -ForegroundColor $MessageTextColour Write-Host "End time : $endTime" -ForegroundColor $MessageTextColour }