kwizcom banner advertisement

Delete Attachments from SharePoint List Items using Powershell

There is a requirement to delete all the attachments named as "xyx.presales.document.docs" from a SharePoint List where 100's of list items already created with lot of attachments. 

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 Salaudeen Rajack on 5:37 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.