Download All Attachments from SharePoint List Items using PowerShell

Requirement: Download all attachments from a SharePoint list to local folder. Lets use PowerShell to download attachments from all SharePoint list items.

download attachment from sharepoint list using powershell

PowerShell to Download Attachments from SharePoint List:

#Site URL and List Name variables
$WebURL = "https://intranet.crescent.com/sites/purchase"    
$LibraryName = "Invoinces"   

#Local folder to which attachments to be downloaded
$DownloadPath = "C:\Docs"     

#Get the web
$Web = Get-SPWeb $WebURL
#Get the Library
$List = $Web.Lists[$LibraryName]    

#Loop through each List item
foreach ($ListItem in $List.Items)
{   
	#Set path to save attachment
	$DestinationFolder = $DownloadPath + "\" + $ListItem.ID          

	#Check if folder exists already. If not, create the folder
	if (!(Test-Path -path $DestinationFolder))        
	{            
		New-Item $DestinationFolder -type directory          
	}
  
	#Get all attachments
	$AttachmentsColl = $ListItem.Attachments

	#Loop through each attachment
	foreach ($Attachment in $AttachmentsColl)    
	{ 
		#Get the attachment File       
		$file = $web.GetFile($listItem.Attachments.UrlPrefix + $Attachment)        
		$bytes = $file.OpenBinary()                

		#Save the attachment as a file  
		$FilePath = $DestinationFolder + " \" + $Attachment
		$fs = new-object System.IO.FileStream($FilePath, "OpenOrCreate")
		$fs.Write($bytes, 0 , $bytes.Length)    
		$fs.Close()    
	}
}

For C# version of the above code, Go to: Download Attachments from SharePoint List Programmatically

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!

2 thoughts on “Download All Attachments from SharePoint List Items using PowerShell

  • where would you use a filter from a column like: ?{$_.Item(“Business Unit”) -eq “Upstream”}

    Reply

Leave a Reply

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