In Focus

Copy Item Attachments To SharePoint Library Using PnP PowerShell

Explains you on how to copy the attachments from a list item to SharePoint library using PnP PowerShell

In this blog, we are going to learn how to copy the attachments from a single list item to SharePoint library using PnP PowerShell.
Check the snippets for downloading attachments and uploading file to library from the nelow links respectively,
The following snippet helps, you to get the attachments from a list item and uploads to the shared Documents library in a current context site.
  1. $cred = Get-Credential    
  2. Connect-PnPOnline -Url https://ktskumartenant.sharepoint.com/sites/dev -Credential $cred  

  3. $listitem = Get-PnPListItem -List Employee -Id 2    
  4. $attachments = ForEach-Object{Get-PnPProperty -ClientObject $listitem -Property "AttachmentFiles"}  
  5.  
  6. $attachments | ForEach-Object { Copy-PnPFile -SourceUrl $_.ServerRelativeUrl –TargetUrl “Shared Documents/$($_.FileName)” }   
After running the powershell command, it asks us to confirm the sourceurl and target url. If the file already exists in the targeturl, we have to add -OverwriteIfAlreadyExists $true to avoid the file already exists error.

Below is the example copies the two attachments from the single list item to the folder within a SharePoint library.