Swap the URLs of two SharePoint Online sites

Or how to remove SharePoint redirect sites the right way!

In some occasions you will want to change the URL of an SharePoint Online site. Maybe the function has changed or the team name for example.
You can do that wiht the PnP PowerShell commandlet: Rename-PnPTenantSite.

When you do so and it succeeds, your site will live on the new URL you specified and the old URL will redirect to that new location as well.
The redirect happens by way of a special SharePoint site template with the template name redirectsite#0.
In case you want to re-use the old URL you will have to remove the redirect site at that address. That can be done with the commandlet Remove-PnPTenantSite.
One catch in the process that I have noticed, is that sometimes (but not always it seems) that if you use the Get-PnPTenantSite commandlet it will either get the redirect site or the site that is redirected to!
If you want to get the actual redirectsite with template redirectsite#0, you can do it by first getting all sites of that template and then filtering the list by the URL.
$redirectSite1 = (Get-PnPTenantSite -Template "REDIRECTSITE#0") | Where-Object { $_.Url -eq $aSiteURL };

Make sure to always check after a site rename that:

  • The site exists and works on the new URL
  • The old site's template is of type redirectsite#0 before removing it.

Should you've come here looking for a script to do a URL swap of two sites, look no further than this PowerShell script to swap site URLs.

Show Comments