PowerShell: Swap SPO site URLs

Stupid markdown, cant get it right :D

# PnP.PowerShell module required;

$tenantAdminUrl = "https://yourtenant-admin.sharepoint.com"; $redirectTemplateName = "REDIRECTSITE#0";

$mainUrl = "https://yourtenant.sharepoint.com/sites/site1"; $tempUrl = "https://yourtenant.sharepoint.com/sites/site1-temp"; $secondaryUrl = "https://yourtenant.sharepoint.com/sites/site2";

Write-Host "Connect with a tenant admin account to the environment."; Connect-PnPOnline -Url $tenantAdminUrl -Interactive;

Write-Host "Changing url $mainUrl to $tempUrl";

Rename-PnPTenantSite -Identity $mainUrl -NewSiteUrl $tempUrl -Wait -ErrorAction Stop;

Write-Host "Validate if the site works as expected at new url $tempUrl";

$input1 = Read-Host "Press any key to continue.";

$redirectSite1 = (Get-PnPTenantSite -Template $redirectTemplateName) | Where-Object { $_.Url -eq $mainUrl };

if ($redirectSite1 -and $redirectSite1.Template -eq $redirectTemplateName) { Write-Host "Removing redirect site $mainUrl";

Remove-PnPTenantSite -Url $mainUrl -SkipRecycleBin -Force -ErrorAction Stop;

Write-Host "Waiting for site deletion to complete...";
Start-Sleep -Seconds 30;

$site1Exists = Get-PnPTenantSite -Url $mainUrl -ErrorAction SilentlyContinue;

if ($site1Exists)
{
    throw "Site $mainUrl has not been deleted yet!! Redo deletion and continue script from here.";
}
else
{
    Write-Host "$mainUrl redirect is removed. Continueing.." -ForegroundColor Green;
}

Write-Host "Changing url $secondaryUrl to $mainUrl";

Rename-PnPTenantSite -Identity $secondaryUrl -NewSiteUrl $mainUrl -Wait -ErrorAction Stop;

Write-Host "Validate if the site works as expected at new url $mainUrl";

$input2 = Read-Host "Press any key to continue.";

$redirectSite2 = (Get-PnPTenantSite -Template $redirectTemplateName) | Where-Object { $_.Url -eq $secondaryUrl };

if ($redirectSite2 -and $redirectSite2.Template -eq $redirectTemplateName)
{
    Write-Host "Removing redirect site $secondaryUrl";

    Remove-PnPTenantSite -Url $secondaryUrl -SkipRecycleBin -Force -ErrorAction Stop;

    Write-Host "Waiting for site deletion to complete...";
    Start-Sleep -Seconds 30;

    $site2Exists = Get-PnPTenantSite -Url $secondaryUrl -ErrorAction SilentlyContinue;

    if ($site2Exists)
    {
        throw "Site $secondaryUrl has not been deleted yet!! Redo deletion and continue script from here.";
    }
    else
    {
        Write-Host "$secondaryUrl redirect is removed. Continueing.." -ForegroundColor Green;
    }

    Write-Host "Changing url $tempUrl to $secondaryUrl";

    Rename-PnPTenantSite -Identity $tempUrl -NewSiteUrl $secondaryUrl -Wait -ErrorAction Stop;

    Write-Host "Validate if the site works as expected at new url $secondaryUrl";

    $input3 = Read-Host "Press any key to continue.";

    $redirectSite3 = (Get-PnPTenantSite -Template $redirectTemplateName) | Where-Object { $_.Url -eq $tempUrl };

    if ($redirectSite3 -and $redirectSite3.Template -eq $redirectTemplateName)
    {
        Write-Host "Removing redirect site $tempUrl";

        Remove-PnPTenantSite -Url $tempUrl -SkipRecycleBin -Force -ErrorAction Stop;

        Write-Host "Switching URLs successfull!" -ForegroundColor Green;
    }
    else
    {
        Write-Host "site3 variable is empty or $tempUrl is not a redirect site. Retry from this point."
    }
}
else
{
    Write-Host "site2 variable is empty or $secondaryUrl is not a redirect site. Retry from this point."
}

} else
{ Write-Host "site1 variable is empty or $mainUrl is not a redirect site. Retry from this point." }