SharePoint Online: How to Change Subsite URL using PowerShell?

Requirement: Change the URL of a subsite in SharePoint Online.

How to Change the Subsite URL in SharePoint Online?

To rename a site URL in SharePoint Online, follow these steps:

  • Click on Settings Gear Icon >> Choose “Site Information” and then “View All Site Settings”
  • Click on “Title, description, and logo” under the “Look and Feel” group
  • Enter the new URL for the subsite and click on “OK”
    sharepoint online change url of subsite

This renames subsite URL in SharePoint Online.

PowerShell to Change Subsite URL in SharePoint Online

Here is the PowerShell to change the URL of a SharePoint Online Subsite.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables
$SiteURL="https://crescenttech.sharepoint.com/sites/marketing/us"
$NewURL="/sites/marketing/UnitedStates" #Relative URL

Try {
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the Sub-Site
    $Web = $Ctx.web
    $Ctx.Load($web)
    $Ctx.ExecuteQuery()
 
    #change subsite url sharepoint online powershell
    $web.ServerRelativeUrl = $NewURL
    $Web.Update()
    $Ctx.ExecuteQuery()
     
    Write-host "Sub-Site URL has been Changed to '$NewURL'" -ForegroundColor Green  
}
Catch {
    write-host -f Red "Error Changing Subsite URL!" $_.Exception.Message
}

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

Leave a Reply