Welcome › Forums › General PowerShell Q&A › Append to multiple filenames in Sharepoint online library
- This topic has 3 replies, 3 voices, and was last updated 2 weeks, 1 day ago by
Participant.
-
AuthorPosts
-
-
December 22, 2020 at 10:38 am #281852
Hello,
We have a SP online library which has thousands of PDF files. I have been tasked with appending ” – SCS” to each filename so thatfilenameD32.pdf
filenameC45.pdf
filename435.pdfwould become
filenameD32 – SCS.pdf
filenameC45 – SCS.pdf
filename435 – SCS.pdfI am the proverbial jack of all trades IT administrator and know powershell to a degree, but would not consider myself a proficient scripter so am after some guidance on connecting to the SharePoint library and renaming these files please.
Thanks
-
December 22, 2020 at 12:45 pm #281888
Have you done any research or tried anything? Here is a link for SP 2016:
SharePoint 2016: Rename File in Document Library Using PowerShell
Haven’t worked with SP in a while, but $ListItem[“Name”] represents the name, so you would just do a -replace .pdf with ‘ – SCS.pdf’. Not sure what what properties are available in the list like file ext or basename. You’d filter if($ListItem[“Name”] -like ‘*.pdf’) or if there is an extension you could use that to filter.
-
December 23, 2020 at 4:05 am #282029
Hi Rob,
Thanks for the reply. Have looked at lots of Google articles etc but most involved connecting SharePoint as a mapped drive or network location and doing it locally, but due to the volume of files the folder shows as empty. A couple of scripts I tried renamed the files but omitted the .pdf. I could download, rename & upload but would rather figure out a way to do it online as I am sure this wil pop up again. I will have a look at your link.
Thanks
John -
January 5, 2021 at 3:16 am #284200
@John W:
If you haven’t found a solution, this should work… I use the SharePointPnPPowerShell module for pretty much any SharePoint Online stuff and it solves your problem very simply (assuming all files to be renamed are in the same directory, otherwise a little more work is needed).
Essentially, get all PDF files in the Document Library, and for each one, append the ” – SCS” to the file name:
PowerShell1234567891011121314151617# Connect to SharePoint Online site$siteUrl = 'https://my.sharepoint.com/sites/Governance'Connect-PnPOnline –Url $siteUrl –UseWebLogin# specify the Document Library (and subfolder path if necessary)$pdfLibrary = "/Shared Documents/pdf"# get all .pdf files and append ' - SCS' to themGet-PnPFolderItem -FolderSiteRelativeUrl $pdfLibrary -ItemType File |Where-Object -Property Name -Match '\.pdf$' |ForEach-Object -Process {$targetFileName = $_.Name -replace '\.pdf$', ' - SCS.pdf'Rename-PnPFile -ServerRelativeUrl $_.ServerRelativeUrl -TargetFileName $targetFileName -ForceWrite-Output ('Renamed "{0}" to "{1}"' -f $_.Name, $targetFileName)}The Rename-PnPFile cmdlet requires only the new file name be specified in the -TargetFileName parameter (do not include the entire location). The -Force is needed to suppress confirmation prompts for each file rename.
-
-
AuthorPosts
- You must be logged in to reply to this topic.