kwizcom banner advertisement

How to Restore Previous Document Version in SharePoint 2016 using PowerShell?

Version history feature in SharePoint enables us to track changes and revert to old versions when turned ON. Any changes made in SharePoint online list item or document, creates new version. There are scenarios you could run into, where you have to roll back to an older version of document. All you have to do is select a version from the version history and simply restore.  

How to Restore a previous document version in SharePoint?
  • Go to your document library and click and Right click on the document name and date, and then click Version History from the menu. 
  • You'll see a list of versions of the file. Hover your mouse over the Modified date column of the version you want to recover. Click on the arrow to get the menu.
    restore previous version in sharepoint
  • Click "Restore" from the version history menu and the confirm by clicking "OK" button.
    PowerShell to Restore a previous document version in SharePoint
This creates a new version from the previous version you choose.

Please note, You may have to "Check Out" prior to restoring the document, restore and then check-i, if Check out is mandatory of the particular document library.

PowerShell to restore a File's Previous version:
Here is the PowerShell script to restore the previous version of a document.
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
 
#Set Configuration Parameters
$WebURL = "http://portal.crescent.com/deals/"
$ListName = "Templates"
$ItemID="3" 

#Get the Web, List, Item and Item objects
$Web= Get-SPWeb $WebURL
$List= $web.Lists[$ListName]
$Item = $List.GetItembyID($ItemID)
 
#Get the File versions
$File = $item.File
$FileVersions = $file.Versions
#Restore the Previous version of the file
$fileVersions.Restore($fileVersions.Count - 1)  
write-host "Restored version: $($FileVersions.Count - 1) on $($File.Name)"

Restore Previous Versions of All Documents in a Library:
If you would like to go to one previous version of all the documents in a SharePoint document library, Use this PowerShell script:
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
 
#Set Configuration Parameters
$WebURL = "http://portal.crescent.com/"
$ListName = "Templates"
 
#Get the Web, List, Item and User objects
$Web= Get-SPWeb $WebURL
$List= $web.Lists[$ListName]
 
#Get all Items from the List
$ListItems = $List.Items
ForEach ($Item in $ListItems)
{
    #Get the File versions
    $File = $Item.File
    $FileVersions = $File.Versions
    
    #If the File has versions
    if ($FileVersions.Count -gt 0)
    {           
        #Restore the previous version
        $FileVersions.Restore($FileVersions.Count - 1)    
        write-host "Restored Previous Version on $($File.Name)"
        #Write-host:"Version Label: $($File.Versions[$(($FileVersions.Count-1))].VersionLabel)"
    }   
}
How to Restore Previous Document Version in SharePoint 2016 using PowerShell? How to Restore Previous Document Version in SharePoint 2016 using PowerShell? Reviewed by Salaudeen Rajack on June 18, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.