Saturday, June 18, 2016

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

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)"
    }   
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...