Retrieve List Items Created in Last N days using PowerShell SharePoint Online

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.

[toc]

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.

sharepoint powershell get list items caml created date
sharepoint powershell get list items caml created date

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:

get items from sharepoint list using powershell script
get items from sharepoint list using powershell script

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.

powershell caml query sharepoint list
get items created before n days from sharepoint list using powershell script

You may like following PowerShell tutorials:

I hope this PowerShell tutorial, we learned how to retrieve List Items Created in Last N days using PowerShell SharePoint Online.

>