kwizcom banner advertisement

Rename a File in SharePoint Document Library using PowerShell

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

PowerShell to Rename a File in SharePoint:
Lets rename a file with ID "4" in "Documents" SharePoint library.
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Variables for processing
$WebURL = "http://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, Lets use the existing file name and rename it.
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Variables for processing
$WebURL = "http://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
Rename a File in SharePoint Document Library using PowerShell Rename a File in SharePoint Document Library using PowerShell Reviewed by Salaudeen Rajack on 1:28 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.