SharePoint Online: Rename Folder using PowerShell

Requirement: Rename a Folder in SharePoint Online.

sharepoint online rename folder using powershell

How to Rename a Folder in SharePoint Online?

To rename a folder in SharePoint Online document library,

  • Open the document library in the browser, Right-click on the folder you want to rename, and then click “Rename”.
    sharepoint online rename folder
  • On Rename pop-up window, type the new name for the folder and then click on the “Save” button.
    sharepoint online powershell rename folder

Please note, Renaming of the SharePoint online folder will have an impact on the URL of the folder! You also get the “Rename” option from the toolbar! Now, let’s see the SharePoint Online PowerShell to rename a folder.

You can also rename a folder by using the Explorer View!

PowerShell to Rename Folder in SharePoint Online

Here is the SharePoint Online PowerShell to rename a folder

#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://crescent.sharepoint.com/sites/marketing"
$FolderURL="/sites/marketing/Project Documents/Active" #Server Relative URLs
$FolderNewURL="/sites/marketing/Project Documents/InActive"

Try {
    $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 Folder
    $Folder = $Ctx.Web.GetFolderByServerRelativeUrl($FolderURL)
    $Ctx.Load($Folder)
    $Ctx.ExecuteQuery()
    
    #Rename Folder
    $Folder.MoveTo($FolderNewURL)
    $Ctx.ExecuteQuery()

    Write-host -f Green "Folder has been renamed to new URL:"$FolderNewURL
}
Catch {
    write-host -f Red "Error Renaming Folder!" $_.Exception.Message
}

SharePoint Online: PnP PowerShell to Rename Folder

With PnP PowerShell script, let’s contract the above script.

#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$FolderURL= "Shared Documents/Reports" #Site Relative URL
$FolderNewName ="Reports Archive"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Rename the Folder
Rename-PnPFolder -Folder $FolderURL -TargetFolderName $FolderNewName

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

12 thoughts on “SharePoint Online: Rename Folder using PowerShell

  • October 18, 2021 at 8:01 PM

    How can I rename multiple folders? I have several folders and should rename them

    Reply
  • October 14, 2021 at 4:17 PM

    The concern now is that it appears the authentication methods have changed. I get an error with this function,. (Get-Credential)

    Reply
  • February 11, 2020 at 5:42 PM

    can we do this for list of ondrive users from a csv file using their onedrive url?

    Reply
  • March 7, 2019 at 1:06 PM

    Put the Library as read only then set permissions on the folder

    Reply
  • February 7, 2019 at 3:25 PM

    Hi i need block the rename of folder. Can you help me, say me how i make that?

    Reply
  • February 17, 2018 at 9:41 AM

    I have 25000 items list in online site, could you please tell me this script will work to delete list colunm.

    Reply
  • February 13, 2018 at 8:44 AM

    It didnt work with relative url, i set the full url and it worked !
    Thanks for the script

    Reply
    • September 1, 2020 at 9:43 PM

      This also isnt working for me with the relative url. How did you use the full URL?

      Reply
    • September 2, 2020 at 5:42 PM

      The CSOM method uses server relative URL! E.g. If you have a site at “https://crescent.sharepoint.com/sites/marketing” then the folder “Active” under “Shared Documents” document library’s server relative URL is: /sites/marketing/Shared Documents/Active”

      Whereas the PnP PowerShell uses site relative URL. So here in this case, the site relative URL of the folder is: /Shared Documents/Active

      Reply

Leave a Reply