Download specified doc version from Sharepoint 2010 library

…this post was published on 2017 January 17.

using Powershell, could be a challenge.

Why?
First you need to use version 2 , and if you already have other greater version installed on the machine you are forced to use only the console ( not the ISE env ) with param -version 2
Second, because is version 2 , you don have the cmdlet Invoke)WEbRequest to download file from specified address
In this case, on option is to use .NET , and define an object from class System.Net.WebClient.
$webclient = New-Object System.Net.WebClient
And because, connection need a user and password, you need to create a credential and attach it to that object
$cred = Get-Credential
$webclient.Credentials = $cred
To download  a file, you will use the method .DownloadFile, like this
$webclient.DownloadFile($sourcepath,$filename)
In my example I will check if the size of the version is greater than a specified value
The entire script to download the needed doc versions, is bellow, where you need to specify your values for
$siteurl =  Sharepoint 2010 site path
$library = The name of the library where docs are stored
$version =  which version of doc you want to check
$filesize = the minimum size in KB for the file
$pathfile = the location where the script will download the docs
 ​################### modify these params as necessary
$siteurl = ‘http://sp2010’
$library=”Library1″
$version = 1
$filesize= 17
$pathfile = “C:\Users\Administrator\Desktop\”
#######################
$cred = Get-Credential
$webclient = New-Object System.Net.WebClient
$site = get-spweb $siteurl
$mylist = $site.Lists[$library]
$items = $mylist.Items
foreach ( $item in $items)
    {
    if (($item.Versions.count -ge $version) -and ( ($item.Versions | where {$_.VersionLabel -eq $version.ToString()+’.0′})[‘File Size’] / 1KB -gt $filesize))
        {
        $sourcepath=$siteurl+’/_vti_history/’+$version*512+’/’+$library+’/’+$item.File.Name
        if($item.Versions.count -eq $version)
            {
            $sourcepath=$siteurl+’/’+$library+’/’+$item.File.Name
            }
        $filename=$pathfile+$item.name
        $webclient.Credentials = $cred
        $webclient.DownloadFile($sourcepath,$filename)
        $item.File.Name
        }
    }
This entry was posted in Powershell, Sharepoint 2010. Bookmark the permalink.