SharePoint Online: Download Attachments from List using PowerShell

Requirement: Download attachments from SharePoint Online List.

How to download attachments from SharePoint List Items?

Do you need to download an attachment from a list in Microsoft SharePoint Online? Perhaps you need to access all documents that have been stored as attachments with list items, or download multiple attachments from a specific list item. Whatever the reason, downloading files from SharePoint can be done in just a few quick steps. Let me show you the fastest and easiest way to download attachments from a SharePoint Online list.

To download attachments of a particular list item, you can open the item in either view or edit mode, right-click on each attachment and choose to save from the browser context menu. In case you want to download all attachments from a list item or all list items, use the “Map Network Drive” feature. Here is how:

  1. Go to Windows Explorer (Windows Key + E), Right-click on “This PC” from the left tree view, and choose “Map Network Drive”.
  2. Select the drive, such as “Z:” and enter the URL of your SharePoint list. E.g., https://intranet.crescent.com/Lists/Credit/.
  3. This opens the Windows explorer view with the attachments folder. Attachments are organized with sub-folders under the attachment folder. Your list item ID becomes the name of the sub-folder! From here, you can download all attachments by simply copy-pasting them to your local machine.
    Download Attachments from List using PowerShel

PowerShell to download attachments from a List Item:

Are you looking to automate the process of downloading files attached to list items in SharePoint Online? Let me show you how to use PowerShell to download attachments from a SharePoint Online list. Let’s programmatically download the SharePoint list item attachment from a particular list item.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Download-ListItemAttachments()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ListItemID,
        [Parameter(Mandatory=$true)] [string] $DownloadPath
    )    
   Try {
        #Setup Credentials to connect
        $Cred = Get-Credential
        $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Get the List Item
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItem= $List.GetItemByID($ListItemID)
    
        #Get All attachments from the List Item
        $AttachmentsColl = $ListItem.AttachmentFiles
        $Ctx.Load($AttachmentsColl)
        $Ctx.ExecuteQuery()
    
        #Get each attachment
        ForEach($Attachment in $AttachmentsColl)
        {
            #Download attachment
            $FileContent = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx, $Attachment.ServerRelativeUrl)
            $FileStream = [System.IO.File]::Create($DownloadPath+$Attachment.FileName)
            $FileContent.Stream.CopyTo($FileStream)
            $FileStream.Close()
       }

        write-host  -f Green "Total List Attachments Downloaded : $($AttachmentsColl.count)"
    }
    Catch {
        write-host -f Red "Error Downloading List Item Attachments!" $_.Exception.Message
    } 
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
$ListItemID="1"
$DownloadPath="C:\Temp\"

#Call the function to copy list items
Download-ListItemAttachments -SiteURL $SiteURL -ListName $ListName -ListItemID $ListItemID -DownloadPath $DownloadPath

Download Attachments from All Items in the List using PowerShell:

Here is the PowerShell to download SharePoint Online list attachments from all items.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Download-ListAttachments()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $DownloadDirectory
    )    
   Try {
        #Setup Credentials to connect
        $Cred = Get-Credential
        $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
    
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Get All Items from the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
        
        #Create download directory if it doesn't exist
        If (!(Test-Path -path $DownloadDirectory))        
        {            
            New-Item $DownloadDirectory -type directory          
        }
        
        #Iterate through each list item
        Foreach($Item in $ListItems)
        {
            #Get All attachments from the List Item
            $AttachmentsColl = $Item.AttachmentFiles
            $Ctx.Load($AttachmentsColl)
            $Ctx.ExecuteQuery()

            #Get attachment for each list item
            ForEach($Attachment in $AttachmentsColl)
            {
                #Download attachment
                $FileContent = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx, $Attachment.ServerRelativeUrl)
                $FileName= $DownloadDirectory+$Item.id.ToString()+"_"+$Attachment.FileName
                $FileStream = [System.IO.File]::Create($FileName)
                $FileContent.Stream.CopyTo($FileStream)
                $FileStream.Close()
           }
        }

        write-host  -f Green "List Attachments Downloaded Successfully!"
    }
    Catch {
        write-host -f Red "Error Downloading List Attachments!" $_.Exception.Message
    } 
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
$DownloadDirectory="C:\Temp\"

#Call the function to copy list items
Download-ListAttachments -SiteURL $SiteURL -ListName $ListName -DownloadDirectory $DownloadDirectory

SharePoint Online: Download Attachments from List Item using PnP PowerShell

This PowerShell downloads all attachments from a given list item.

#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Projects"
$ListName = "Docs"
$ItemID = "1"
$DownloadPath = "C:\Temp\"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the List Item
$Listitem = Get-PnPListItem -List $ListName -Id $ItemID

#Get Attachments from List Item
$Attachments = Get-PnPProperty -ClientObject $Listitem -Property "AttachmentFiles"

#Download All Attachments from List item
Write-host "Total Number of Attachments Found:"$Attachments.Count

$Attachments | ForEach-Object { 
    Get-PnPFile -Url $_.ServerRelativeUrl -FileName $_.FileName -Path $DownloadPath -AsFile 
}

Download Attachments from SharePoint Online List using PnP PowerShell

Let’s download all attachments from a list. This script creates a folder for each list item and downloads all attachments from it. 

#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Projects"
$ListName = "Project Docs"
$DownloadPath = "C:\Temp\"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all List Items
$Listitems = Get-PnPListItem -List $ListName -PageSize 500

#Iterate through List Items
ForEach($Item in $Listitems)
{
    #Get Attachments from List Item
    $Attachments = Get-PnPProperty -ClientObject $Item -Property "AttachmentFiles"

    #Download All Attachments from List item
    Write-host "Downloading Attachments from List item '$($Item.ID)', Attachments Found: $($Attachments.Count)"

    #Create directory for each list item
    $DownloadLocation = $DownloadPath+$Item.ID
    If (!(Test-Path -path $DownloadLocation)) { New-Item $DownloadLocation -type Directory | Out-Null }

    $Attachments | ForEach-Object { 
        Get-PnPFile -Url $_.ServerRelativeUrl -FileName $_.FileName -Path $DownloadLocation -AsFile -Force
    }
}

This extracts all attachments from all list items to the local disk.

In summary, we discussed various methods for downloading attachments from a SharePoint Online list. Downloading attachments from a SharePoint Online list is a useful way to extract important files from your SharePoint list for backup or other purposes. By following the steps outlined in this guide, you can quickly and easily extract important files from your SharePoint list for backup or other purposes.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

12 thoughts on “SharePoint Online: Download Attachments from List using PowerShell

  • Thanks, the mapped drive option worked fine for me, as I just needed to archive some attachments from a list we were no longer using. Excellent, thanks!

    Reply
  • I am looking for a way to download only a subset of the list, e.g. from ID 1 to ID 999. What needs to be adjusted in the script to make this work?

    Reply
    • Just filter the list items: $Listitems = Get-PnPListItem -List $ListName -PageSize 500 | Where {$_.ID -ge 1 -and $_.ID -le 999 }

      Reply
  • Any way to upload multiple attachments using list id. I mean the other way round?

    Reply
  • any idea on how to upload multiple attachments to different list items.
    thanks

    Reply
  • When dealing with list items with multiple attachments, how can I download these attachments organized by the list item they were under?

    Reply
    • This script creates a folder for each list item based on list item ID and then downloads all attachments inside the folder!

      Reply
  • Thank you so much!!!! mapping the drive was just what I was looking for

    Reply
  • Can i change id to another filed here ??

    Reply
    • Yes, If the field is unique! Otherwise, when a same attachment file name found in more than one list item, that may gets overwritten.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *