How to get a specific date range file count using PowerShell

Viewed 79

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
2 Answers

Assuming Created is a datetime type object, for a range of 120 days around today, you can try that :

$rangestart = (Get-date).AddDays(-60)
$rangeEnd = (Get-date).AddDays(60)
$FolderItems.items() | where-object {($_.Created -gt $rangestart) -and ($_.Created -lt $rangeEnd)}

If $folderItems is a powershell Array just remove the .items() before the pipe.

I have reproduced in my environment, and thanks to @StephenP and followed his SO-thread :

$upperBound = [datetime]::ParseExact('08/04/2022 01:00:00',                                    'dd/MM/yyyy hh:mm:ss',
                                   [Cultureinfo]::InvariantCulture)

$lowerBound = [datetime]::ParseExact('30/07/2022 01:00:00', 'dd/MM/yyyy hh:mm:ss',                              [Cultureinfo]::InvariantCulture)

(Get-ChildItem "X" -Recurse) | Where-Object {($_.LastWriteTime -gt  $upperbound) -and ($_.LastWriteTime -lt $lowerbound)}

X- X means path of the files

enter image description here

Now if you want count, you can follow like below:

enter image description here

Related