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

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 is time-consuming and cumbersome from web UI, 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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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