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.

PowerShell  to Filter List Items based on Created or Modifed Date SharePoint Online

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.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “SharePoint Online: Filter List Items based on Created or Modified Date using PowerShell

  • Hi Salaludeen,

    My requirement is to move the csv files that older than n days to another sharepoint folder in the same sile but when i filtering Get-PnPFolderItem it shows all the contents in the sharepoint folder, could you please help me to filter files based on the date modified ?
    $cutOffDate = (Get-Date).AddDays(-2)
    Get-PnPFolderItem -FolderSiteRelativeUrl $SourceLibraryURL | Where-Object { $_.Name -like “*.csv” -and $_.LastModifiedDateTime -lt $cutOffDate }

    Reply
  • Hi, is there a version for SharePoint 2013?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *