Copy previous version of SharePointOnline

Pradeep 1 Reputation point
2022-12-28T08:13:53.107+00:00

For an Unknow reason we have many files in Teams / SharePointOnline file store with Zero Bytes.
the previous Version of the files have the correct content.
Trying to find a powershell script to copy the older version to a new place to evaluate correctness.

Please help

Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft Teams | Microsoft Teams for business | Other
{count} votes

5 answers

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,606 Reputation points Microsoft External Staff
    2022-12-28T10:22:57.977+00:00

    Hi @Pradeep ,
    You can restore previous versions of all files in the vault using PnP PowerShell:

    #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 Library in batches  
            $AllItems = Get-PnPListItem -List $Library -PageSize $BatchSize -Fields ID -ScriptBlock { Param($items) $global:counter += $items.Count; `  
                    Write-Progress -PercentComplete ($global:Counter / ($Library.ItemCount) * 100) -Activity "Getting List Items of '$($_.Title)'" `  
                         -Status "Processing Items $global:Counter to $($Library.ItemCount)";}  | Where {$_.FileSystemObjectType -eq "File"}  
            Write-Progress -Activity "Completed Retrieving Items from List $($Library.Title)" -Completed  
               
            #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://domain.sharepoint.com/sites/sitename"  
    $ListName = "LibraryName"  
       
    #Connect to SharePoint Online  
    Connect-PnPOnline -Url $SiteURL -Interactive  
       
    #Get the Library  
    $Library = Get-PnPList -Identity $ListName  
       
    #Call the function to Restore Previous Versions of all files  
    $Library | Restore-PreviousVersion  
    

    274555-12281.png
    *
    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.

    0 comments No comments

  2. Pradeep 1 Reputation point
    2022-12-28T18:08:57.677+00:00

    YanliJiangMSFT Thanks a lot
    I was hoping we can copy the file to a separate location instead of restoring the file
    Thanks
    Ppprasad


  3. Pradeep 1 Reputation point
    2023-01-03T08:28:57.69+00:00

    Thanks a lot, You have explained well,
    Additional Thought
    Can i copy all the files in the present state to another Location with its previous vesrions, where i can do a rertore with out touching the original error loaction


  4. Pradeep 1 Reputation point
    2023-01-03T10:17:43.843+00:00

    @Yanli Jiang - MSFT
    Thanks a lot for all your help
    I see this in the PNP documentation and have not tried this
    any thoughts on this?

    https://pnp.github.io/powershell/cmdlets/Copy-PnPFile.html

    -IgnoreVersionHistory
    If provided, only the latest version of the document will be copied and its history will be discarded. If not provided, all historical versions will be copied.

    Type: SwitchParameter
    Parameter Sets: (All)

    Required: False
    Position: Named
    Default value: None
    Accept pipeline input: False
    Accept wildcard characters: False

    0 comments No comments

  5. Yanli Jiang - MSFT 31,606 Reputation points Microsoft External Staff
    2023-01-05T11:16:32.75+00:00

    Hi @Pradeep ,
    Based on the reference materials you gave, I tested it, and yes, it can meet your needs.
    The steps as below:
    Run the code use pnp PowerShell as an admin:
    Connect-PnPOnline -url "https://domain.sharepoint.com/sites/sitename"
    Copy-PnPFile -SourceUrl "/sites/sitename/sourcelibraryname/filename.docx" -TargetUrl "/sites/sitename/targetlibraryname"
    276419-01050.png
    Then the file is copy to the other library:
    276458-01051.png
    276466-01052.png
    Now, you can restore the version history.
    *
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.