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?

Folders in Microsoft SharePoint Online helps to keep your content organized. Do you need to rename a folder in SharePoint Online? This blog post will show you how to rename a folder in SharePoint Online. This process is simple and can be completed in a few easy steps.

To rename a folder in a SharePoint Online document library, do the following:

  1. Open the document library in the browser, Right-click on the folder you want to rename, and then click “Rename”. (Or you can select a folder, and click on the “Rename” button in the toolbar!)
    sharepoint online rename folder
  2. On Rename pop-up window, type the new name for the folder and then click on the “Save” button.

The folder will be renamed immediately. Please note, Renaming the SharePoint Online folder will impact the URL of the folder!

Now, let’s see 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 how you can rename a folder in SharePoint Online using PowerShell:

#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 the PnP PowerShell, let’s contract the above script. Here is the SharePoint Online PnP PowerShell to rename a folder:

#Config Variables
$SiteURL = "https://Crescent.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

Similarly, to rename a document library in SharePoint Online, use: SharePoint Online: PowerShell to Rename a List or Document Library

Can you rename a SharePoint online site?

Yes! You can change both the Name of the site and the site URL with the SharePoint Admin center or PowerShell script.
More info: How to Rename SharePoint Online Site?

How do I change the URL of a document library in SharePoint Online?

To rename SharePoint online document library URL, You can use File Explorer: Open the document library in File Explore View >> Navigate One Level Up in the Windows File Explorer >> Select the Document Library to Rename >> Right Click and choose “Rename” >> Enter the new Name for your document library. You can also use PowerShell scripts to change the URL of a SharePoint Online list or library.
More info: Rename SharePoint Online document library URL

How to rename a file in SharePoint Online?

Navigate to the library where the file to rename is stored >> Right-click on your desired File and Choose “Rename” >> Provide a new name to the file in the Rename pop-up and click Save. You can also use the PowerShell CSOM methods or Rename-PnPFile cmdlet to rename a file in SharePoint Online.
More info: How to rename a File in SharePoint Online Document Library?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

13 thoughts on “SharePoint Online: Rename Folder using PowerShell

  • what is the limitation number of items for renaming a folder?

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

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

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

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

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

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

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

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

      Reply
    • 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

Your email address will not be published. Required fields are marked *