Friday, 4 January 2013

Powershell script to Delete Document By ID in SharePoint 2010


Below is the Powershell script to delete document by ID 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].id -eq 777) # 777 is the ID of the document
    {
      Write-Host("DELETED: " + $listItems[$x].name)
      $listItems[$x].Delete()
    }
  }

No comments:

Post a Comment