SharePoint - migrate a sub-site into its own Site Collection

# Move-SPwebToNewSPsite.ps1
#
# simple outline sample of how to migrate a sub-site into its own new Site Collection.
# definately not comprehensive, but should give you an idea of what to do.
# NO I have not exported Alerts or Workflows. Food for thought!
#

Add-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$ExportShare = "\\SERVERNAME\Backup\oldWeb.CMP"
$OldSPWebUrl = "https://intranet.contoso.com/SubSite"

$newMgdPath = "Sites"
$newSPSiteName = "TheNewSite"
$Descr = "the newSite collection"

try {

  $oldSPWeb = Get-SPWeb $oldSPwebUrl

  $SPWebApp = $oldSPWeb.Site.WebApplication
  $oldSubWebUrl = $OldSPWeb.ServerRelativeUrl
  $Owner = $OldSPWeb.AssociatedOwnerGroup.Users[0].LoginName

  Export-SPweb -Identity $oldSPWeb -Path $ExportShare

  $newSiteUrl = "{0}{1}/{2}" -f $SPWebApp.Url, $newMgdPath, $newSPSiteName
 
  if (-NOT (get-spmanagedpath -Identity $newMgdPath -WebApplication $SPWebApp)) {
   new-spmanagedpath -RelativeURL $newMgdPath -WebApplication $SPWebApp
  }

  $newSPSite = New-SPsite -Url $newSiteUrl -OwnerAlias $Owner -Template "STS#0" -Description $Descr
  $newSPWeb  = $newSPSite.RootWeb
  Import-SPweb $newSPWeb -Path $ExportShare

  Remove-SPweb $oldSPWeb -Confirm:$false
 }

 catch {
  "Error caught: {0}" -f $Error[0]
 }