How to get a specific date range file count using PowerShell

Aase Nomad 246 Reputation points
2022-09-07T18:16:28.497+00:00

I'm using PNP Module to get my OneDrive file and folder total items count. The below script is working fine but I'm just wondering if there is a way to get only a certain date range file and folder count.

For example, I just want an item total count between CreatedDate 03/03/2011 to CreatedDate 08/31/2022 instead of everything.

Any help or suggestion would be really appreciated.

   $SiteURL = ""  
   $ListName = "Documents"  
          
   Connect-PnPOnline $SiteURL -Credentials $credential  
     
   #Get the list  
   $List = Get-PnPList -Identity $ListName | Select Title, ItemCount  
   $global:counter = 0  
         
   $FolderItems = Get-PnPListItem -List $ListName -PageSize 500  -Fields "FileLeafRef", "Created","Modified", "SMTotalFileStreamSize","FileRef","File_x0020_Type" -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete `  
       ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Items from List:" -Status "Processing Items $global:Counter to $($List.ItemCount)"; }  | Where { $_.FileSystemObjectType -eq "folder" }  
     
   $FolderItems.Count  
SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,206 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,560 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,655 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 44,621 Reputation points
    2022-09-07T19:05:48.153+00:00

    You should be able to get the items by adding a CAML query to the Get-PnPListItem cmdlet.

    To query by date ranges with this: caml-query-search-between-two-date-ranges-sharepoint

    0 comments No comments

  2. RaytheonXie_MSFT 30,906 Reputation points Microsoft Vendor
    2022-09-08T08:27:26.057+00:00

    Hi @Aase Nomad
    I do agree with RichMatheisen-8856's answer. You can add a camlquery when Get-PnPListItem. Please refer to following script

    $Query =   
       "<Where>  
          <And>  
             <Geq>  
                <FieldRef Name='Created' />  
                <Value Type='DateTime'>2011-03-03T12:00:00Z</Value>  
             </Geq>  
             <Leq>  
                <FieldRef Name='Created' />  
                <Value Type='DateTime'>2022-08-31T12:00:00Z</Value>  
             </Leq>  
          </And>  
       </Where>";   
    $FolderItems = Get-PnPListItem -List $ListName -Query $Query   
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.