Download Attachments From SharePoint List Item Using PnP PowerShell

In this blog, you'll learn how to use the PnP PowerShell to download the attachments from the specified list item.
 
Check this link for downloading multiple files from the given URL using the same PnP PowerShell.
 
Step 1

First, we have to get the list item from SharePoint.
  1. $listitem = Get-PnPListItem -List <ListName> -Id <Item ID>  
Step 2

Get the attachment for the list item.
  1. $attachments = ForEach-Object{Get-PnPProperty -ClientObject $listitem -Property "AttachmentFiles"}  
Step 3

Download the retrieved attachments from the following path.
  1. $attachments | ForEach-Object { Get-PnPFile -Url $_.ServerRelativeUrl -FileName $_.FileName -Path "E:\Downloads\" -AsFile }  
The below code will authenticate against SharePoint and retrive the list item from Employee list based on the ID 2. And then, it retrieves the collection of attachments and downloads those attachments to the local path "E:\ktskumar\Documents".
  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. $attachments | ForEach-Object { Get-PnPFile -Url $_.ServerRelativeUrl -FileName $_.FileName -Path "E:\ktskumar\Documents" -AsFile }  
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now