3

Does anybody know if there's some way to delete items from an SPList using PowerShell scripting?

If is this possible, has anybody any good tutorial?

Thanks!!!

2 Answers 2

4

Since you can access the object model it's pretty straight forward

asnp microsoft.sharepoint.powershell
$web = Get-SPWeb http://thesite/
$list = $web.Lists["NameOfList"]
$item = $list.GetItemById(1) 
$item.Delete()
2
  • ok. now, should I just save it as .ps and then run it from PS??
    – hello B
    Commented Mar 6, 2012 at 20:04
  • You just open a new powershell session and write the code...and yes, you could make it into a .ps1 script file if you need... Commented Mar 7, 2012 at 14:32
2

Here's the example.

Quote from link:

Delete Item -

The example below will count the items and loop down, read the name of the item, and if the item contains a 3, then it will delete that item.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite("http://test.tomdaly.com") # is a legit url
$relweburl = "/Docs"
$web = $site.openweb($relweburl)
$list=$web.Lists["testList"] $listItems = $list.Items
$listItemsTotal = $listItems.Count

for ($x=$listItemsTotal-1;$x -ge 0; $x--)
{
    if($listItems[$x].name.Contains("3"))
    {
        Write-Host("DELETED: " + $listItems[$x].name)
        $listItems[$x].Delete()
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.