You could use following PowerShell to restore previous versions of files from a folder.
#Function to Restore Previous Versions of all Files in a Library
Function Restore-PreviousVersion
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.Client.List]$Library
)
Begin
{
$BatchSize = 2000
$global:Counter = 0
}
Process
{
#Get All Files from the folder in batches
$AllItems = Get-PnPFolderItem -FolderSiteRelativeUrl $ParentFolderURL -ItemName $FolderName
#Process All Files from the Library
$global:Counter = 1
ForEach($Item in $AllItems)
{
#Get File and Versions from the List Item
Get-PnPProperty -ClientObject $Item -Property File | Out-Null
Get-PnPProperty -ClientObject $Item.File -Property Versions | Out-Null
If($Item.File.Versions.Count -gt 0)
{
#Get the previous Version's Label
$VersionLabel = $Item.File.Versions[$Item.File.Versions.Count-1].VersionLabel
Write-Host "$(Get-Date) - ($($Counter)/$($AllItems.Count)) - Restoring version $VersionLabel for $($Item.File.Name)"
$item.File.Versions.RestoreByLabel($VersionLabel)
Invoke-PnPQuery
}
else
{
Write-Host "$(Get-Date) - ($($Counter)/$($AllItems.Count)) - Skipping $($Item.File.Name) as there are no previous versions!"
}
$Counter++
}
}
end
{
}
}
#Set Parameters
$SiteURL = "https://tenant.sharepoint.com/sites/test"
$ParentFolderURL = "/Shared Documents"
$FolderName = "foldername"
#Connect to SharePoint Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Call the function to Restore Previous Versions of all files
$Library | Restore-PreviousVersion
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.