During development I often need to recreate a site collection in a SharePoint Online tenant. Usually this is simply deleting the old site and creating the site again. The following script would automate these steps, using the Office 365 PnP PowerShell extensions.

But be careful, the existing site collection will be removed without any confirmation.

When the site collection was created, the Connect-PnPOnline cmdlet is called to connect to the new site collection.

param (
	[Parameter(Mandatory=$true)]
    [string]$Tenant,
	[Parameter(Mandatory=$true)]
    [string]$WebUrl,
	[Parameter(Mandatory=$true)]
	[string]$Title
)

$url = "https://$Tenant.sharepoint.com/sites/$WebUrl"
$owner = "admin@$Tenant.onmicrosoft.com"
$lcid = 1033
$template = "STS#0"
$timezone = 4
$resourceQuota = 10
$storageQuota = 1

Try
{
	Write-Host -ForegroundColor Yellow "Checking, if site already exists..."
	
    $site = Get-PnPTenantSite -Url $url -ErrorAction SilentlyContinue
}
Catch
{
}

if ($site -ne $null)
{
    Write-Host -ForegroundColor Yellow "Site already exist, removing current site..."

    Remove-PnPTenantSite -Url $url -Force
	
	Write-Host
}

Write-Host -ForegroundColor Yellow "Creating new site..."

New-PnPTenantSite -Title $Title -Url $url -Owner $owner -Lcid $lcid -Template $template -TimeZone $timezone -ResourceQuota $resourceQuota -StorageQuota $storageQuota -Wait -RemoveDeletedSite

Write-Host

Connect-PnPOnline -Url $url

Write-Host -ForegroundColor Green "Done."

Advertisements