Delete Attachments from SharePoint List Items using Powershell

Requirement: Delete Attachment from SharePoint List Item

How to Remove Attachment from a List Item in SharePoint?
There is a requirement to delete all the attachments named as "xyz.presales.document.docx" from a SharePoint List where 100's of list items already created with lot of attachments. Attachments can be removed from list item by doing the following:
  1. Navigate to the list where you want to remove attachments.
  2. Select the list item where attachment to be removed and click on "Edit Item" from the ribbon.
  3. On the Edit Item page, click the delete option next to the attachment to be deleted. Confirm the prompt.
  4. On the Edit Item page, click the Save button.
The attachment is deleted from the item.


Remove Attachments from SharePoint List Items using PowerShell:
This requirement can be achieved with either SharePoint Object model or with PowerShell. Here is the script to programmatically delete the attachments based on its name.

$web=Get-SPWeb -identity "http://sharepointsite.com"
$list=$web.lists["Documents"]
foreach($item in $List.Items)
  {
        for($i=$item.Attachments.count-1; $i -ge 0; $i--) 
        { 
             if($item.Attachments[$i].endswith("presales.document.docx"))
             {
                 write-host "File Deleted:" $item.Attachments[$i]
                 $Item.Attachments.Delete($item.Attachments[$i])
              }
        }  
           $item.update()
  }
Delete Attachments from SharePoint List Items using Powershell Delete Attachments from SharePoint List Items using Powershell Reviewed by Unknown on August 04, 2012 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.