PowerShell Script to delete items from SharePoint List

The criteria for the below script is to delete items that created before 7 days , you can put it as scheduale task to run weeklly to clear the large lists.

Add-PSSnapin Microsoft.SharePoint.PowerShell
[System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
$web = GetSPWeb “YOUR SHAREPOINT SITE”
$list = $web.Lists[“YOUR LIST NAME”]
$DeleteBeforeDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([DateTime]::Now.AddDays(7))
$caml=‘<Where> <Lt> <FieldRef Name=”Created” /><Value Type=”DateTime”>{0}</Value> </Lt> </Where> ‘ $DeleteBeforeDate
$query=new-object Microsoft.SharePoint.SPQuery
$query.Query=$caml
$col=$list.GetItems($query)
WriteHost $col.Count
$col | % {$list.GetItemById($_.Id).Delete()}
$web.Dispose()

Courtesy: https://gallery.technet.microsoft.com/office/PowerShell-Script-to-f4e8ea90

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s