Powershell Tips Series : Download all attachments from a SharePoint list


Sriram Varadarajan
Enterprise Architect
Published On :   19 Dec 2015
Visit Count
Today :  2    Total :   8314
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


Use the below Poweshell script to download all the attachments from a SharePoint list / library and save those files to local folder

Code Snippet

 $webUrl = "URL"
 $library = "Documents"  
 #Local Folder to dump files
 $tempLocation = "D:\attachments"     
 $s = new-object Microsoft.SharePoint.SPSite($webUrl)    
 $w = $s.OpenWeb()         
 $l = $w.Lists[$library]    
 foreach ($listItem in $l.Items)
 {
     Write-Host "    Content: " $listItem.ID 
     $destinationfolder = $tempLocation + "\" + $listItem.ID          
     if (!(Test-Path -path $destinationfolder))        
     {            
         $dest = New-Item $destinationfolder -type directory          
     }
     foreach ($attachment in $listItem.Attachments)    
     {        
         $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)        
         $bytes = $file.OpenBinary()                
         $path = $destinationfolder + "\" + $attachment 
         Write "Saving $path" 
         $fs = new-object System.IO.FileStream($path, "OpenOrCreate") 
         $fs.Write($bytes, 0 , $bytes.Length)    
         $fs.Close()    
     }
 }
 
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Forms App