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

Requirement: Filter and Get List Items based on its 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 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 its 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"]) }
SharePoint Online: Filter List Items based on Created or Modifed Date using PowerShell SharePoint Online: Filter List Items based on Created or Modifed Date using PowerShell Reviewed by Salaudeen Rajack on February 21, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.