Excel played a little trick on me removing 0 and 00 in front of subsites URL for a migration I did.

I migrated subsites from on premise farm to online using a CSV, but without noticing, excel removed the zeros in front of the subsites URL.

So I now have 400 subsites that had a 0 or 00 in front like this (original on premise subsite url- /sites/0012345

and imported to this subsite online to- /sites/12345/ removing the two zeros in front.

I'm trying to automate this as I have 400+ subsite to rename, using PnP or any suggestion welcome.

So I want to rename all 400+ sites online adding them back the 0 or 00 in front.

Found this script online but don't want to use this 400 times, would there be an easier way?

Your help is greatly appreciated. Thanks

$OldSiteURL = "https://crescent.sharepoint.com/sites/ops"
$NewSiteUrl = "https://crescent.sharepoint.com/sites/operations"
#Connect to SharePoint Online from PnP PowerShell
Connect-PnPOnline -Url $AdminSiteURL -Interactive
#Change sharepoint online site url powershell
Rename-PnPTenantSite -Identity $OldSiteURL -NewSiteUrl $NewSiteUrl
  • create a mapping in csv file (i.e. old url and new url). Read data from this CSV. Loop over each row and get the respective input and perform update. Nov 18, 2022 at 8:14
  • shared the sample code .... you can refer it Nov 18, 2022 at 8:26
  • 1
    Thanks, really a good start, this helped me a lot. See answer below, that ended up working for our tenant.
    – Mathieu
    Nov 18, 2022 at 16:56
  • I am glad it helped. Nov 18, 2022 at 17:50

2 Answers 2

$inputCSV     = "./InputToUpdateUrl.csv"  #specify path of your CSV file here
$siteDetails  = Import-CSV $inputCSV
$adminSiteUrl = "https://crescent-admin.sharepoint.com"

Connect-PnPOnline -Url $AdminSiteURL -Interactive

$siteDetails | ForEach-Object{

    $oldSiteUrl = $_.OldSiteUrl 
    $newSiteUrl = $_.NewSiteUrl
    Rename-PnPTenantSite -Identity $OldSiteURL -NewSiteUrl $NewSiteUrl  

Refer below Image for same CSV:

enter image description here


Thanks for the help Rohit, this indeed was I was looking to do.

Unfortunatly, found out that the Rename-PnpTenantSite function does not work on Multi-Geo tenant, such as ours. Also, the interactive login didn't work, so ended up using the -UseWebLogin instead.

I also ended up looking at other scripts and here is what worked, a combination of your script + the below :

$inputCSV     = "c:/temp/test.csv"  #specify path of your CSV file here
$siteDetails  = Import-CSV $inputCSV

$siteDetails | ForEach-Object{
    $SiteURL = $_.OldSiteUrl  #csv column A
    $NewURL = $_.NewSiteUrl   #csv column B 

    Connect-PnPOnline -Url $SiteURL -UseWebLogin

    $Web = Get-PnPWeb
    $Web.ServerRelativeUrl = $NewURL

    Write-host "Subsite has been Renamed!" -ForegroundColor Green

Note, in the CSV, you need to use the following full url for the OldSiteUrl and relative path for the NewSiteUrl as seen below :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.