SharePoint 2016: Rename File in Document Library Using PowerShell

In this post, we will learn how to Rename File in Document Library Using PowerShell in SharePoint 2016 and SharePoint 2013.


How to Rename File in Document Library Using PowerShell in SharePoint 2016?

In this section, we will provide two PowerShell script to do the following:

  1. Rename File in Document Library Using PowerShell.
  2. Replace a word in the file name of all files inside the document library

Rename File in Document Library in SharePoint 2016 using PowerShell

To change the file name for a file in a document library in SharePoint 2016 using PowerShell, you should follow the below steps:

Steps

  • Open SharePoint Managment Shell as Administrator.
Open SharePoint 2016 PowerShell - Upgrade and Migrate to Project Server 2016
  • Edit the following code with your entries ($Web,$DocLib,$filename).
  • Paste it in SharePoint Management Shell > Enter.
$Web = Get-SPWeb "http://epm/workflow"
$DocLib = $Web.Lists["Doc"] #your Doc Library Name
$filename = "Qassas.pdf" #the file name that you need to change
foreach($ListItem in $DocLib.Items)
{
if($ListItem["Name"] -eq $filename)
{
if( $ListItem.CheckOutStatus -ne "None" ) #if the file is not checked out
{
  $ListItem.File.CheckOut() #check the file out
  $ListItem["Name"] = "Your New Name" #set the new file name
  $ListItem["Title"] = "Your New Title" #set the new file title
  $ListItem.Update()
  $ListItem.File.CheckIn("File Name Updated") #check in
}
ELSE # if the file is checked out
{
  $ListItem["Name"] = "Your New Nameqq"
  $ListItem["Title"] = "Your New Title"
  $ListItem.Update()
  $ListItem.File.CheckIn("File Name Updated") }
} }
Rename File in Document Library Using PowerShell in SharePoint 2016
Rename File in Document Library Using PowerShell
  • Go back to the Doc library > Select the file > View Properties.
  • The File Name and its Title should be now renamed properly as shown below:
rename-file

Replace a specific word in the file name using PowerShell

If you need to replace a specific word at your file name within document library in SharePoint via PowerShell, you should follow the below steps:

Steps

  • Open SharePoint Managment Shell as Administrator.
Open SharePoint 2016 PowerShell - Upgrade and Migrate to Project Server 2016
  • Edit the following code with your entries ($Web,$DocLib,$filename).
  • Paste it in SharePoint Management Shell > Enter.
$Web = Get-SPWeb "http://epm/workflow"
$DocLib = $Web.Lists["Doc"] #your Doc Library Name
$filename = "Qassas 2015.pdf" #the file name that you need to change
foreach($ListItem in $DocLib.Items)
{
if($ListItem["Name"] -eq $filename)
{
if( $ListItem.CheckOutStatus -ne "None" ) #if the file is not checked out
{
$ListItem.File.CheckOut() #check the file out
$ListItem["Name"] = $ListItem["Name"].replace("2015","2016") #replace
$ListItem["Title"] = $ListItem["Title"].replace("2015","2016")
$ListItem.Update() $ListItem.File.CheckIn("File Name Updated") 
#check in } 
ELSE # if the file is checked out 
{ $ListItem["Name"] = $ListItem["Name"].replace("2015","2016")
  $ListItem["Title"] =$ListItem["Title"].replace("2015","2016")
  $ListItem.Update() $ListItem.File.CheckIn("File Name Updated") } 
} }

Applies To
  • SharePoint 2016
  • SharePoint 2013
See Also

Leave a Reply

Scroll to Top