What PowerShell script can rename folders or files in a folder/file path on a SharePoint Online Document Library, this way?

frob 4,216 Reputation points
2022-10-08T21:14:52.213+00:00

Hi there
What PowerShell script can rename folders or files in a SharePoint Online Document Library, taking CurrentURLs and NewURLs from a CSV file?
The renaming may need to be done anywhere in the path, it could be a new name for a folder/subfolder/file.

Sample CSV file. Bold items need to be renamed:
CurrentURL, NewUrl
https://abc.sharepoint.com/sites/docs/folder/**subfolder**/file1.docx, https://abc.sharepoint.com/sites/docs/folder/**subfolder2**/file1.docx
https://abc.sharepoint.com/sites/docs/**finance**/subfolder/file1.docx, https://abc.sharepoint.com/sites/docs/**finance2**/subfolder/file1.docx
https://abc.sharepoint.com/sites/docs/finance/subfolder/**file1**.docx, https://abc.sharepoint.com/sites/docs/finance/subfolder/**file2**.docx

Thanks.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,792 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,396 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xuyan Ding - MSFT 7,561 Reputation points
    2022-10-10T07:10:30.95+00:00

    Hi @frob ,

    You could follow the code below to rename each location. The FolderURL and FileURL below can be changed to folder or subfolder paths.

    $SiteURL = "https://abc.sharepoint.com/sites/MeredithTest"  
    $FolderURL= "lib1/a/11"   
    $FolderNewName ="aa"  
    $FileURL= "lib1/a/aa/Document3.docx"  
    $NewFileName ="wenjian.docx"  
           
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  
           
    #Rename the Folder  
    Rename-PnPFolder -Folder $FolderURL -TargetFolderName $FolderNewName  
           
    #Rename the File  
    Rename-PnPFile -SiteRelativeUrl $FileURL -TargetFileName $NewFileName -Force  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful