Posted by: R Manimaran | February 24, 2014

PowerShell: Copy SharePoint List Attachments to Document Library

I have a SharePoint List which contains huge number of items. Most of the items will have attachments associated to it. I want to copy all the attachments and upload it to a document Library in the same site collection.

PowerShell script made my req very simple. Below powershell script will loop through all the list items and process its attachment.

$web = $site.RootWeb<br />
$Lists = $Web.Lists[$ListName]
 $Library = $web.Lists[$LibName]
 foreach($listItem in $Lists.Items)
      if($listItem.Attachments.Count -gt 0)
           Write-Host "**************************************************"
           Write-Host $listItem.Attachments.Count"Attachment(s) available in the ListItem:" $listItem.Title
           Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
           foreach($attachment in $listItem.Attachments)<br />
               Write-Host $i".Attachment Name:" $listItem.Attachments.UrlPrefix$attachment
               $file = $web.GetFile($listItem.Attachments.UrlPrefix+$attachment)
               Write-Host "Adding Files to Library:"$Library.Title
               $bytes = $file.OpenBinary()
               $path = $Library.RootFolder.Url+"/"+$attachment
               $disFile = $Library.RootFolder.Files.Add($path,$bytes,$true)
               Write-Host "Successfully Added"<br /> Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
        Write-Host "**************************************************"

The powershell can be downloaded in the below location.

Click here to download


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: