SharePoint Online: Download All Versions of a Document using PowerShell

Requirement: Download All Versions of a Document in SharePoint Online using PowerShell.

Download All Versions of a Document in SharePoint Online using PowerShell

How to download the previous version in SharePoint Online?

To download a specific version of a document in SharePoint Online, you can follow these steps using the web interface:

  1. Access the SharePoint Online site’s document library through a web browser.
  2. Right-click on the document you want to download a version of >> Select “Version History” from the menu.
  3. Hover over the date/time of the document version you wish to download for review >> Click the document link to open the document.
    download version history sharepoint
  4. Now, You can download the selected version by using the “Save As” option in the menu of the client application, such as Microsoft Word.

SharePoint Online: PowerShell to Download All Versions of a Document

In SharePoint Online, documents are stored in libraries, and it’s common for multiple versions of a document to exist over time. Version history in SharePoint Online lets you store, track, and restore files in a library as they are changed. Sometimes, you may need to download all previous versions of a file. However, downloading all versions of a document can be a tedious process, especially if you have to do it manually. To save time and effort, you can use PowerShell to download all versions of a document in SharePoint Online.

As downloading each version of a file from web UI is time-consuming and cumbersome, here is the PowerShell to download all versions of a file from a SharePoint Online document library using CSOM.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$FileRelativeURL ="/Shared Documents/Discloser China.doc"
$DownloadPath= "C:\Temp"

#Get credentials to connect
$Cred = Get-Credential

#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)
 
#Get the File and its versions
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
$Ctx.Load($File)
$Ctx.Load($File.Versions)
$Ctx.ExecuteQuery()
 
If($File.Versions.Count -gt 0)
{
    Foreach($Version in $File.Versions)
    {
        #Frame File Name for the Version
        $VersionFileName = "$($DownloadPath)\$($Version.VersionLabel)_$($File.Name)"
        
        #Get Contents of the File Version
        $VersionStream = $Version.OpenBinaryStream()
        $Ctx.ExecuteQuery()

        #Create File version at local disk
        [System.IO.FileStream] $FileStream = [System.IO.File]::Open($VersionFileName,[System.IO.FileMode]::OpenOrCreate)
        $VersionStream.Value.CopyTo($FileStream)
        $FileStream.Close()
        
        Write-Host -f Green "Version $($Version.VersionLabel) Downloaded to :" $VersionFileName
    }
}
Else
{
    Write-host "No Versions Available for "$Item["FileRef"] -f Yellow
}

PnP PowerShell to Export All Versions of a Document

Let’s use PnP PowerShell to download all versions of a file from SharePoint Online:

#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Funds"
$FileRelativeURL = "/sites/Funds/Funds/Reporting/External/QR/2019/Q3 2019/Global 4/Global IT Seg v2.xlsx"
$DownloadPath = "C:\Temp"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$Ctx = Get-PnPContext

#Get the File
$File = Get-PnPFile -Url $FileRelativeURL

#Get File Versions
$FileVersions = Get-PnPProperty -ClientObject $File -Property Versions

If($FileVersions.Count -gt 0)
{
    Foreach($Version in $FileVersions)
    {
        #Frame File Name for the Version
        $VersionFileName = "$($DownloadPath)\$($Version.VersionLabel)_$($File.Name)"
         
        #Get Contents of the File Version
        $VersionStream = $Version.OpenBinaryStream()
        $Ctx.ExecuteQuery()
 
        #Download File version to local disk
        [System.IO.FileStream] $FileStream = [System.IO.File]::Open($VersionFileName,[System.IO.FileMode]::OpenOrCreate)
        $VersionStream.Value.CopyTo($FileStream)
        $FileStream.Close()
         
        Write-Host -f Green "Version $($Version.VersionLabel) Downloaded to :" $VersionFileName
    }
}
Else
{
    Write-host -f Yellow "No Versions Found!"
}

In summary, using PowerShell to download all versions of a document in SharePoint Online is a convenient and efficient way to get every change made to your documents. With PowerShell script, you can quickly retrieve all versions of a document, making it easier to track changes and keep a record of the document’s history.

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!

Leave a Reply

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