SharePoint Online: Filter List Items based on Created or Modified Date using PowerShell
Requirement: Filter and Get List Items based on their created or modified date values in SharePoint Online using PowerShell.
How to Filter and Get List Items Created in the Past 30 Days using PowerShell?
Here is the PowerShell example to get list items created in the past 30 days in the SharePoint Online list.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
Try {
#Get Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List
$List = $Ctx.Web.lists.GetByTitle($ListName)
#Define CAML Query to Filter
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXML = "<View>
<Query>
<Where>
<Gt>
<FieldRef Name='Created' Type='DateTime'/>
<Value Type='DateTime'>
<Today OffsetDays='-30'/>
</Value>
</Gt>
</Where>
</Query>
</View>"
#Get List Items
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
Write-host -f Green "Number of List Items Found:"$ListItems.Count
#Get Each Item's Created Date
$ListItems | ForEach-Object { Write-host ("List Item:{0} was created on {1}" -f $_["FileLeafRef"],$_["Created"]) }
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
PnP PowerShell to Filter List Items Modified in the Past 24 Hours:
Similarly, we can filter list items based on the last modified date with PnP PowerShell, as:
#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/marketing/"
$ListName = "Documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Define Query to Filter
$Query= "<View>
<Query>
<Where>
<Gt>
<FieldRef Name='Modified' Type='DateTime'/>
<Value Type='DateTime' IncludeTimeValue='TRUE'>
<Today OffsetDays='-1'/>
</Value>
</Gt>
</Where>
</Query>
</View>"
$ListItems = Get-PnPListItem -List $ListName -Query $Query
Write-host "Total Number of Items Found:"$ListItems.count
#Get Each Item's Modified Date
$ListItems | ForEach-Object { Write-host ("List Item:{0} was Modified on {1}" -f $_["FileLeafRef"],$_["Modified"]) }
In summary, retrieving a list of items in SharePoint Online based on their created or modified date can be easily accomplished with the help of PowerShell. By following the steps outlined in this article, you can use PowerShell to quickly and efficiently retrieve a list of items based on their creation or modification date.
Hi, is there a version for SharePoint 2013?
Here is an Example: How to use CAML Query in SharePoint On-Premises?