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 Answer
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]
For more details Check Change File Name in SharePoint Document Library Via PowerShell
-
Thanks M.Qassas. The extra file checkout functionality makes sense. I have some examples that use web.dispose at the end. Is that necessary?– DkongSep 13, 2016 at 4:02
-
1Dear @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