Saturday, October 5, 2013

Delete All SharePoint List Items Using PowerShell 2010

How to delete all list items from SharePoint list?

Follow the steps to delete all list items from SharePoint List

1. Create new PowerShell (.ps1) file with below script using Notepad

$Url = "http://site-url:5000"
$ListName = "Sales 2013"
$Web = Get-SPWeb $Url
$List = $Web.lists[$ListName]

if($List -eq $null)
{    
  Write-Error "The List cannot be found";return
}

Write-Warning "Deleting all list items from $($ListName)"
$Items = $List.GetItems()
Write-Host "Total Items to be deleted : $($Items.count)"

if($Items.count -gt 0)
{
  $shell = new-object -comobject wscript.shell
  $result = $shell.popup("Total Item to be deleted : $($Items.count) `nDo   you want to continue?",0,"Alert",4+32)

if($result -eq "6")
{
foreach ($item in $Items)
{
  $itemId = $item.ID
  $List.GetItemById($itemId).Delete()
  Write-Host "Deleted list item with id $($itemId)"
}
}
}
$List.Update()
$Web.Dispose()


2. Replace values of parameters $Url and $ListName

3. Save file in server hard drive (Example: D:\PowerShell\Delete-List-Items.ps1)

4. Open "SharePoint 2010 Management Shell" with "Run as administrator"

5. Navigate to the folder where the script  file is stored
    (Example: cd D:\PowerShell)

6. Select file to be executed
    (Example: .\Delete-List-Items.ps1)

7. Press enter and enjoy!!

No comments: