Find all files on SharePoint Online between two date

Copper Contributor

I am trying to write a script in PowerShell that copies all my files from one site to another site. I am trying to keep costs down by only taking so many files at the same time. I figured I would do this with a date range on Created Date. But I cannot get it to use date to filter the list nor can it get it to sort the list by Created. Here is what I have tried:

Get-PnPListItem -List $ListName  -PageSize 500  | Where-object {$_.FieldValue.Created -GT [DateTime]'02/16/2015'} 
    
Get-PnPListItem -List $ListName -PageSize 500 | where-object {($_.FileSystemObjectType -eq "File" -and $_.Created -gt [DateTime]'02/16/2015')} 
    
Get-PnPListItem -List $ListName -PageSize 500 | where-object { $_.FileSystemObjectType -eq "File" -and $_['Created_x0020_Date'] -gt $Created } 
    
$ListItems = $ListItems | Sort $_.Created # also tried $_['Created_x0020_Date']

If I use a CAML query I always get the exceed the list view threshold error even with defined in the query.
$Query = "<View> <RowLimit Paged = 'TRUE'>100 <Query> <Where> <And> <Geq> <FieldRef Name='Created' /> <Value Type='DateTime'>2015-01-01T12:00:00Z</Value> </Geq> <Leq> <FieldRef Name='Created' /> <Value Type='DateTime'>2015-01-30T12:00:00Z</Value> </Leq> </And> </Where> </Query> </RowLimit> </View>"; Get-PnPListItem -List $ListName -PageSize 500 -Query $Query

 Any help would be greatly appreciated.

3 Replies

Hi @YosiP,

your idea (or your goal) is really interesting.

I cannot test it, but I think it is better to download and use the script in SharePoint Online Management Shell, you can download it here:
Download SharePoint Online Management Shell from Official Microsoft Download Center

you can try this script using SharePoint Management Shell to copy files from one SharePoint Online site to another based on a date range:

# Define your source and destination SharePoint Online sites
$sourceSiteUrl = "https://yoursource.sharepoint.com/sites/SourceSite"
$destinationSiteUrl = "https://yourdestination.sharepoint.com/sites/DestinationSite"

# Connect to the source SharePoint site
Connect-SPOService -url $sourceSiteUrl -Credential (Get-Credential)

# Specify the source and destination libraries or lists
$sourceLibraryName = "SourceLibrary"
$destinationLibraryName = "DestinationLibrary"

# Specify the date range
$startDate = "2022-01-01"
$endDate = "2022-12-31"

# Connect to the destination SharePoint site
Connect-SPOService -url $destinationSiteUrl -Credential (Get-Credential)

# Get files from the source library within the date range
$files = Get-SPOSite $sourceSiteUrl | Get-SPOWeb | Get-SPOList $sourceLibraryName | Get-SPOListItem | Where-Object {
($_.FileSystemObjectType -eq "File") -and ($_.FieldValues.Created -ge $startDate) -and ($_.FieldValues.Created -le $endDate)
}

# Copy files to the destination library
foreach ($file in $files) {
$fileName = $file.FieldValues.FileLeafRef
$fileBytes = $file.OpenBinary()
$destinationPath = "/sites/DestinationSite/$destinationLibraryName/$fileName"

# Upload the file to the destination library
Set-Content -Path $destinationPath -Value $fileBytes -Force
Write-Host "Copied file: $fileName"
}

# Disconnect from SharePoint Online
Disconnect-SPOService

 

Replace the values (`https://yoursource.sharepoint.com/sites/SourceSite`, `https://yourdestination.sharepoint.com/sites/DestinationSite`, `SourceLibrary`, `DestinationLibrary`, `2022-01-01`, `2022-12-31`) with your actual SharePoint URLs, library or list names, and the desired date range. 

Ensure you have the necessary permissions on both sites.

 

Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.


If the post was useful in other ways, please consider giving it Like.


Kindest regards,


Leon Pavesic

@LeonPavesic I had to uninstall and reinstall the sharepoint module. Even after that I got this message:

 

$files = Get-SPOSite $sourceSiteUrl | Get-SPOWeb | Get-SPOList $sourceLibraryName | Get-SPOListItem | Where-Object {
($_.FileSystemObjectType -eq "File") -and ($_.FieldValues.Created -ge $startDate) -and ($_.FieldValues.Created -le $endDate)
}
Get-SPOWeb : The term 'Get-SPOWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:39
+ $files = Get-SPOSite $sourceSiteUrl | Get-SPOWeb | Get-SPOList $sourc ...
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-SPOWeb:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

YosiP_0-1695135027731.png

 



I am getting help on another forum. Thank you for all your help.