-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete List Items by Date.ps1
77 lines (62 loc) · 4.34 KB
/
Delete List Items by Date.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#Script that deletes items in a list, limited by threshold defined in Sharepoint
# Configuration variables
$WebURL = "http://XXXXXXXXXXXXX"
$ListName = "Last Added Items"
$Threshold = 5000
$RecycleFlag = $false
### DO NOT TOUCH THE CODE BELOW ###
# Required if not run in SharePoint Management Shell
if($ver.Version.Major -gt 1)
{
$Host.Runspace.ThreadOptions = "ReuseThread"
}
If((Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.Sharepoint.Powershell
}
$DeleteBeforeDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([DateTime]::Now.AddDays(-60))
$web = Get-SPWeb $WebURL;
If(!($web)) { Write-Output "Website not found. Verify URL" -ForegroundColor Red; Break;}
Else { Write-Output "URL is valid: $($web)"}
$list = $web.Lists[$ListName]
If(!($list)) { Write-Output "List not found. Verify URL" -ForegroundColor Red; Break;}
Else { Write-Output "List has $($list.itemCount) items"}
While ($list.ItemCount -ne 0)
{
Write-Output "Querying started - $(Get-Date -Format o)"
$query = New-Object Microsoft.Sharepoint.SPQuery;
$query.RowLimit = $Threshold;
$caml = '<Where>
<Lt>
<FieldRef Name="Created" />
<Value Type="DateTime">{0}</Value>
</Lt>
</Where>' -f $DeleteBeforeDate
$query.Query = $caml
$items = $list.GetItems($query)
Write-Output "Querying completed - $(Get-Date -Format o)"
If($items.Count -eq 0)
{
Write-Output "List is empty";
Exit 1;
}
Else
{
Write-Output "Deleting started - $(Get-Date -Format o)"
Foreach($item in $items)
{
Write-Output "Deleting item $($item.Id)..."
If($RecycleFlag -eq $true)
{
$list.GetItemById($item.ID).Recycle();
}
Else
{
$list.GetItemById($item.ID).Delete();
}
}
Write-Output "Deleting completed - $(Get-Date -Format o)"
Write-Output "Deleted $Threshold items"
}
}
$web.Dispose();