1

How would I use powershell to rename a pdf file in a document library? I've seen examples online that move files or rename while moving but not an example where the files are renamed in place (ie to keep them in the same document library)

1
  • Title or actual file Name?
    – Benny Skogberg
    Sep 11, 2016 at 8:14

1 Answer 1

3
  • Open SharePoint Management Shell as Administrator.

  • Copy this code , Edit it with your entries ($Web,$DocLib,$filename) , Paste it in SharePoint Management Shell

$Web = Get-SPWeb "http://epm/workflow"
$DocLib = $Web.Lists["Doc"]          
$filename = "Qassas.pdf"
 foreach($ListItem in $DocLib.Items)
   {
     if($ListItem["Name"] -eq $filename) 
       {

             $ListItem.File.CheckOut()
             $ListItem["Name"] = "Your New Name"
             $ListItem["Title"] = "Your New Title"
             $ListItem.Update()
             $ListItem.File.CheckIn("File Name Updated")
       }
   }

[Output]

enter image description here

For more details Check Change File Name in SharePoint Document Library Via PowerShell

2
  • Thanks M.Qassas. The extra file checkout functionality makes sense. I have some examples that use web.dispose at the end. Is that necessary?
    – Dkong
    Sep 13, 2016 at 4:02
  • 1
    Dear @Dkong , once you close the PowerShell, the memory is freed so don't worry if you do not use dispose. especially in a simple script, but if you use a big script that loop to web and lists and you are working in production , make sure to dispose of SPWEB , SPSITE. please don't hesitate to tell me if you need more clarification . Sep 13, 2016 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.