kwizcom banner advertisement

SharePoint Online: Restore Previous Version of a Document using PowerShell

How to Restore a previous version of a document or Item in SharePoint Online?
  • Go to your SharePoint online document library and click on the ellipsis icon next to the document title.
  • From the context menu pop-up, Click on the ellipsis again and then choose "Version History" menu item.
  • In version history page, Hover your mouse over the "Modified" date of the version you want to restore and click the arrow icon to get its context menu.
  • Click "Restore" and confirm the prompt with Yes to restore the particular version of a document.
    powershell to restore previous version in sharepoint online
    SharePoint Online Restore Previous Version
SharePoint Online: Restore a Previous Version of All Items in a Document Library using PowerShell
While its straightforward and simple in SharePoint Online to restore previous version, lets restore previous version of all documents in a document library with PowerShell.
#Import SharePoint Online module
Import-Module Microsoft.Online.SharePoint.Powershell

Function Restore-PreviousVersion()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )
   Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get all items from the list/library
        $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $Ctx.Load($List)
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()

        #Iterate through each item and restore previous version
        Foreach($Item in $ListItems)
        { 
            #Get the file versions
            $File = $Ctx.Web.GetFileByServerRelativeUrl($Item["FileRef"])
            $Ctx.Load($File)
            $Ctx.Load($File.Versions)
            $Ctx.ExecuteQuery()

            If($File.Versions.Count -gt 0)
            {
                #Get the previous version's label
                $VersionLabel=$File.Versions[($File.Versions.Count-1)].VersionLabel

                #Restore previous version
                $File.Versions.RestoreByLabel($VersionLabel)
                $Ctx.ExecuteQuery()
                Write-Host -f Green "Previous version $VersionLabel Restored on :" $Item["FileRef"]
            }
            Else
            {
                Write-host "No Versions Available for "$Item["FileRef"] -f Yellow
            }
        }
     }
    Catch {
        write-host -f Red "Error Removing User from Group!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Documents"

#Call the function to restore previous document version
Restore-PreviousVersion -SiteURL $SiteURL -ListName $ListName

Here is my another post for SharePoint On-Premises to restore previous version of a file using PowerShell: How to Restore Previous Version of a File using PowerShell
SharePoint Online: Restore Previous Version of a Document using PowerShell SharePoint Online: Restore Previous Version of a Document using PowerShell Reviewed by Salaudeen Rajack on 2:24 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.