Friday 4 January 2013

Powershell script to Delete Document By Name in SharePoint 2010


Below is the Powershell script to delete document by Name from Document Library

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite("http:// sharepoint-site-url ")
$web = $site.openweb()
$list=$web.Lists["Document-Library-Name "]
$listItems = $list.Items
$listItemsTotal = $listItems.Count
Write-Host $listItemsTotal
for ($x=$listItemsTotal-1;$x -ge 0; $x--)
{
  if($listItems[$x].name.Contains("file")) # file refers to the    name of the document
  {
    Write-Host("DELETED: " + $listItems[$x].name)
    $listItems[$x].Delete()
  }
}

3 comments:

  1. Hi There,

    This script doesn't seem to work. No errors are reported at all.

    Thanks,
    Steven

    ReplyDelete
  2. Its works for me ! ? Thanks for the script !

    ReplyDelete