Home >Tips >Download all file versions from SharePoint kalmstrom.com site map icon Site map  

Download all File Versions from SharePoint

SharePoint tutorial by Jayant Rimza

SharePoint logoSharePoint is a great platform for file versioning, and it is not uncommon that organizations wish to see or save all versions of a document or use an earlier version to re-create the document.

Here we share a PowerShell script that downloads all versions of all files in a SharePoint document library. You can use the same approach for CSOM(client site object model), JSOM (JavaScript object model) or any other SharePoint object model.

PowerShell iconBelow is the code with explanations. Please study them before you download the script and change the site URL and document library name to your own.

Code description

  1. Get the SharePoint site reference.

    $spSiteURL = "http://intranet.contoso.com/sites/jayant/"
    $spWeb = Get-SPWeb -Identity $spSiteURL

  2. Get the document library as folder. Here “Docs” is the name of the document library, and I load it as SharePoint folder.

    $spDocFolder = $spWeb.GetFolder("Docs")

  3. Load all files in the document library folder

    $spFileCollection = $spDocFolder.Files

  4. Initialize the download object array and its properties. It will be used to store the names and URLs of the downloaded files.

    $dlProps = @{
        DownloadURL = ''
        DownloadFileName = ''
    }
       $dlobjects = @()

  5. Loop through all files in the document library and use file object to find all versions and latest version.

    ForEach($file in $spFileCollection){

  6. The versions property returns a collection of version objects and then loops through all versions to find version number, download URL and file name.

    $spFileVersionCollection= $file.Versions;
    #===============get URLs of old versions===============

    if ($spFileVersionCollection) {
             ForEach($version in $spFileVersionCollection){
                $downloadversURL = $version.Url;
                $dlobject = New-Object -TypeName PSObject -Property $dlProps
                $dlobject.DownloadURL=$downloadversURL;
                $dlobject.DownloadFileName = $version.VersionLabel + "_" + $file.Name;
                $dlobjects +=$dlobject;
             }
    #===============get URLs of old versions===============

  7. The versions collection returns only the previous versions of each file, not the current or latest version. To download the latest version of each file you can use the ($file) file object.

    #==============get URLs of latest versions=============
        $downloadlatestURL = $file.Url;
        $dlobject = New-Object -TypeName PSObject -Property $dlProps
        $dlobject.DownloadFileName = "Latest" + $file.UIVersionLabel + "_" + $file.Name;
        $dlobject.DownloadURL=$downloadlatestURL;
        $dlobjects +=$dlobject;
    #==============get URLs of latest versions=============

  8. The $dlobjects array has the object of all file names and URLs to download and can be used to download all files with the System.Net.WebClient method to download files.

    #================download version files================
    $destination="C:\Users\jayant\Desktop\version files\"
    $webclient = New-Object System.Net.WebClient
    $webclient.UseDefaultCredentials = $true
    ForEach($dlobj in $dlobjects){
        $fullURL = $spSiteURL + $dlobj.DownloadURL;
        $destinationFullPath=$destination + $dlobj.DownloadFileName;
        $webclient.DownloadFile($fullURL, $destinationFullPath);
    }
    #================download version files================

The whole code


$spSiteURL = "http://intranet.contoso.com/sites/jayant/"
$spWeb = Get-SPWeb -Identity $spSiteURL
$spDocFolder = $spWeb.GetFolder("Docs")
$spFileCollection = $spDocFolder.Files
$dlProps = @{
    DownloadURL = ''
    DownloadFileName = ''
}
$dlobjects = @()
ForEach($file in $spFileCollection){
$spFileVersionCollection= $file.Versions;
#===============get URLs of old versions===============
    if ($spFileVersionCollection) {
         ForEach($version in $spFileVersionCollection){
            $downloadversURL = $version.Url;
            $dlobject = New-Object -TypeName PSObject -Property $dlProps
            $dlobject.DownloadURL=$downloadversURL;
            $dlobject.DownloadFileName = $version.VersionLabel + "_" +$file.Name;
           $dlobjects +=$dlobject;
         }
    }
#===============get URLs of old versions===============

#================get URLs of latest versions==========
    $downloadlatestURL = $file.Url;
    $dlobject = New-Object -TypeName PSObject -Property $dlProps
    $dlobject.DownloadFileName = "Latest" + $file.UIVersionLabel + "_" +$file.Name;
   $dlobject.DownloadURL=$downloadlatestURL;
    $dlobjects +=$dlobject;
#================get URLs of latest versions==========
}
#================download version files================
$destination="C:\Users\jayant\Desktop\version files\"
$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
ForEach($dlobj in $dlobjects){
    $fullURL = $spSiteURL + $dlobj.DownloadURL;
    $destinationFullPath=$destination + $dlobj.DownloadFileName;
    $webclient.DownloadFile($fullURL, $destinationFullPath);
}
#================download version files================