Search This Blog

Wednesday, February 7, 2018

Delete SharePoint List-item using PowerShell

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
write-host "This will delete data, type YES to continue"
$retval = read-host
if ($retval -ne "YES")
{
    write-host "exiting - you did not type yes" -foregroundcolor green
    exit
}
write-host "continuing,Please enter site name"
$url = read-host
$web = get-spweb $url
write-host "Please enter list name"
$listName = read-host
$list = $web.lists | where {$_.title -eq $listName}

if ($list.items.count -ne 0)
{
$items = $list.items
foreach ($item in $items)
{
    Write-host "  Say Goodbye to $($item.id)" -foregroundcolor red
    $list.getitembyid($Item.id).Delete()
}
write-host "$($list.title) List item has been deleted successfully"
}else{
Write-host "List $($list.title) has $($list.items.count) entries or List doesn't exit"

}


Save with one folder(.PS1 and below .bat) bat and run with admin in powershell

cd /d %~dp0
powershell -noexit -file "DeleteAllListItem.PS1" "%CD%"
pause

No comments:

Post a Comment