Rename a File in SharePoint Document Library using PowerShell

Requirement: Rename a File in the SharePoint document library using PowerShell.

PowerShell to Rename a File in SharePoint:

Renaming files is one way to keep your documents organized and easy to find. In this blog post, we’ll look at how to use PowerShell to rename files in SharePoint.

Let’s rename a file with ID “4” in the “Documents” SharePoint library.

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Variables for processing
$WebURL = "https://intranet.crescent.com"
$ListName = "Documents"
$ItemID ="4"
$NewFileName="sharepoint databases.pptx"

#Get Web, List and Item
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]
$ListItem = $List.GetItemByID($ItemID)

#Check-out and Rename the file
$ListItem.File.CheckOut()
$ListItem["Name"] = $NewFileName
$ListItem.Update()
$ListItem.File.CheckIn("File has been renamed!") 

Instead of Item ID, let’s use the existing file name and rename it.

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Variables for processing
$WebURL = "https://intranet.crescent.com"
$ListName = "Documents"
$FileName="sharepoint databases.pptx"
$NewFileName="sharepoint databases v2.pptx"

#Get Web, List and Item
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]
$ListItem = $List.Items | Where {$_["Name"] -eq $FileName}

If($ListItem -ne $NULL)
{
    #Check-out and Rename the file
    $ListItem.File.CheckOut()
    $ListItem["Name"] = $NewFileName

    #change the Title of the file name
    $ListItem["Title"] = $NewFileName

    $ListItem.Update()
    $ListItem.File.CheckIn("File has been renamed!")
    Write-Host "File has been Renamed!" -f Green
}
else
{
    Write-Host "File Not Found!" -f Yellow
}

Here is the post for SharePoint Online: SharePoint Online: Rename Files in Document Library using PowerShell

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!

Leave a Reply

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