Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$SiteURL = "http://mySharepoint"
$site = new-object Microsoft.SharePoint.SPSite($SiteURL)
$web= $site.OpenWeb()
$documentLibrary = $web.Lists["Your List or Library Name"]
#-10 means files which are older than 10 days will get deleted. so give the count based on your requirement
$yesterdaysDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([System.DateTime]::UtcNow.AddDays(-10.0))
echo $yesterdaysDate
$query = new-object Microsoft.SharePoint.SPQuery
$query.ViewFields = "<FieldRef Name='Modified' />"
$query.Query = "<Where><Leq><FieldRef Name='Modified' /><Value Type='DateTime' IncludeTimeValue='TRUE'>" + $yesterdaysDate + "</Value></Leq></Where>"
$queryItems = $documentLibrary.GetItems($query)
$querycount = $queryItems.Count;
echo $querycount
for ($intIndex = $querycount - 1; $intIndex -gt -1; $intIndex--)
{
$queryItems.Delete($intIndex);
}
$web.Dispose()
$site.Dispose()This code snippet helps on how to delete the list/library items where the created or modified days are older than a given 'n' number of days using PowerShell script.
