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

The 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 create a new version. You could run into scenarios where you have to roll back to an older version of the document, and all you have to do is select a version from the version history and simply restore it.

How to Restore a previous document version in SharePoint?

Follow these steps to restore the previous version of a document:

  1. Go to your document library and click and Right-click on the document name and date, and then click Version History from the menu.
  2. 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
  3. Click “Restore” from the version history menu and confirm by clicking the “OK” button.
    PowerShell to Restore a previous document version in SharePoint

This creates a new version from the earlier 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 = "https://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 = "https://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)"
    }   
}

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “How to Restore Previous Document Version in SharePoint 2016 using PowerShell?

  • How about
    Restore Previous Versions of All Documents in a Sharepoint Folder?

    Thank you

    Reply
    • Do something like this:

      #Get the Web
      $web = Get-SPWeb -Identity $WebURL
      
      #Get the Folder
      $Folder =  $web.GetFolder($FolderUrl)
      
      #Iterate through each file in the Folder
      foreach ($File in $Folder.Files) 
      {
         #Call the function to clear versions
      }
      
      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *