This PowerShell SharePoint tutorial, we will discuss how to retrieve list items created in the last N days using PowerShell in SharePoint Online.
Recently in a business requirement, we need to find records created within 10 days and also we were in need to find records that are created after 10 days. We can use PowerShell to retrieve list items based on filter criteria in SharePoint online.
Retrieve list items created in last N days using PowerShell SharePoint Online
Here we will see how to retrieve list items created in the last 7 days using PowerShell SharePoint online. We will use CAML to filter the records based on the created date. Here I have a list which has few items in it and you can see two items created recently and one item created before 20 days. Our requirement here is to retrieve items created before 7 days.

Here we will use PowerShell to retrieve the items. In PowerShell, we have used CAML and we add filter using OffsetDays=-7 like below:
$caml = '<View><Query><Where>
<Gt>
<FieldRef Name="Created" Type="DateTime"/>
<Value Type="DateTime">
<Today OffsetDays="-7" />
</Value>
</Gt>
</Where></Query></View>'
Below is the full PowerShell script like below:
You can run, debug and test the script using Visual studio code or by using Windows PowerShell ISE.
Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password=ConvertTo-SecureString "************" -AsPlainText -Force
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$Web = $ctx.Web
$List = $web.get_lists().getByTitle("TrainingInformations")
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery;
$caml = '<View><Query><Where>
<Gt>
<FieldRef Name="Created" Type="DateTime"/>
<Value Type="DateTime">
<Today OffsetDays="-7" />
</Value>
</Gt>
</Where></Query></View>'
$Query.ViewXml =$caml
$ListItems = $list.GetItems($Query);
$ctx.Load($ListItems)
$ctx.ExecuteQuery()
$ListItems | ForEach-Object {
write-host $_["Title"]
}
Once you run the PowerShell script, you can see it will return items created within 7 days like below:

Retrieve list items created before last N days using PowerShell SharePoint Online
Now we will see how we can retrieve how to retrieve items that are created after the last 7 days. It will not return items that are created within the last 7 days.
Here we have modified a little bit the CAML query, instead of Gt we are using Lt and the CAML looks like below:
$caml = '<View><Query><Where>
<Lt>
<FieldRef Name="Created" Type="DateTime"/>
<Value Type="DateTime">
<Today OffsetDays="-7" />
</Value>
</Lt>
</Where></Query></View>'
Below is the full PowerShell code:
Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password=ConvertTo-SecureString "********" -AsPlainText -Force
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$Web = $ctx.Web
$List = $web.get_lists().getByTitle("TrainingInformations")
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery;
$caml = '<View><Query><Where>
<Lt>
<FieldRef Name="Created" Type="DateTime"/>
<Value Type="DateTime">
<Today OffsetDays="-7" />
</Value>
</Lt>
</Where></Query></View>'
$Query.ViewXml =$caml
$ListItems = $list.GetItems($Query);
$ctx.Load($ListItems)
$ctx.ExecuteQuery()
$ListItems | ForEach-Object {
write-host $_["Title"]
}
Once you run the script, you can see the result like below, it will show one item which is create before 7 days.

You may like following PowerShell tutorials:
- Get SharePoint document library size using PowerShell
- PowerShell SharePoint Online: The remote server returned an error: (403) Forbidden
- SharePoint Online Automation: Upload Files Remotely to SharePoint Online Document Library using PowerShell
- Retrieve all list names and list guids from SharePoint online site using PowerShell
- SharePoint 2016 PowerShell Script to list all Users in Site Collection
- SharePoint 2013 backup and restore using PowerShell
- Create web application in SharePoint 2016 from Central Administration and using PowerShell
- Delete list items created before N days using PowerShell in SharePoint Online
- SharePoint Online Create Workflow History List using PowerShell or using SharePoint Designer 2013
- SharePoint Online check if File exists or not in document library using PowerShell
I hope this PowerShell tutorial, we learned how to retrieve List Items Created in Last N days using PowerShell SharePoint Online.
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com