Forum Discussion
Find all files on SharePoint Online between two date
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
- YosiPSep 20, 2023Copper ContributorI am getting help on another forum. Thank you for all your help.