Download All Versions of a Document in SharePoint using PowerShell – Web Services

Requirement:
I wanted to extract and download each version of the document stored in a SharePoint library. Well, PowerShell can do it from the client-side using web services. Here is the PowerShell script to download each version to the local folder programmatically.

Download all versions of a document using PowerShell

# ******* Variables Section ******************
#Define these variables 
$WebURL="https://sharepoint.crescent.com/sites/Operations/"
$FilePath ="https://sharepoint.crescent.com/sites/Operations/docs/designDoc.docx"
$TargetFolder = "C:\Downloads"
# *********************************************

#Web Service URL
$WebServiceURL =$WebURL+"_vti_bin/Versions.asmx"
$WebService = New-WebServiceProxy -Uri $WebServiceURL -UseDefaultCredential
$WebService.URL=$WebServiceURL

#Get File name from file path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("/")+1,($FilePath.Length-$FilePath.LastIndexOf("/")-1))

#Create the Target Library if it doesn't exists
if (!(Test-Path -path $TargetFolder))
{   
        #If it doesn't exists, Create
        $TargetFolder = New-Item $TargetFolder -type directory
}

#Call web service method "GetVersions" to retrieve versions collection
$FileVersions = $WebService.GetVersions($FilePath).Result
foreach($File in $FileVersions)
{ 
    #Frame the File name : E.g. 1.0_Filename.ext
    $VersionFileName = "$($TargetFolder)\$($File.version)_$($FileName)"
    write-host $VersionFileName
    $webclient = New-Object System.Net.WebClient
    $webclient.UseDefaultCredentials = $true
    write-host $File.url
    $webclient.DownloadFile($File.url,$VersionFileName)
    Write-Host "Downloaded version: $($File.Version)"
}

Here is what the output looks like:

PowerShell to Download All Versions of a Document in SharePoint

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!

3 thoughts on “Download All Versions of a Document in SharePoint using PowerShell – Web Services

  • Hi, How can we download all documents and meta data with versioning in sharepoint library.

    Reply
  • I was receiving 401 unauthorized error so I changed default credential to following and it worked perfectly.

    $Credential = New-Object System.Management.Automation.PSCredential(‘BCsmuhammad’,(Read-Host “Enter Password” -AsSecureString))

    $WebService = New-WebServiceProxy -Uri $WebServiceURL -Credential $Credential

    $webclient.Credentials = $Credential

    Reply
    • Yes, If the account runs the script doesn’t have access, You should supply it!

      Reply

Leave a Reply

Your email address will not be published.