Recently while broswing the forums a user requested something interesting; They wanted to have a script that would go through a library and download all major versions of a document that where inbetween a date range.

Interested in this I went to work on a PowerShell script that would do what was needed, and below is the results

 

 

PowerShell
Edit|Remove
$startDate = (Get-Date 01/01/2001) 
$endDate = (Get-Date 01/01/2001) 
 
$web = Get-SPWeb "http://site" 
$list = $web.lists["Library"$items = $list.items 
 
 Write-Host "Looking for files, Please wait..." 
 
 foreach($item in $items) 
 { 
    $versionHistory = $item.Versions 
 
    foreach($version in $versionHistory) 
    { 
       if($version.VersionLabel -match "[0-9]+.0") 
       { 
 
        $versioncreated = $version.created 
     
        if($versionCreated -gt $startDate -and $versionCreated -lt $endDate) 
        { 
            Write-Host "" 
            Write-Host "Item Name:" $item.Name -ForegroundColor Yellow 
            Write-Host "Item version found" -ForegroundColor Green -NoNewline ;Write-Host " Version Created: $versioncreated" -ForegroundColor Cyan 
             
            $destination = "C:\temp\Downloads" + "\" + $version.VersionLabel + " " + $item.Name 
            $documentURL = $web.Url + "/" + $item.url 
 
            $Wc = New-Object System.Net.WebClient 
            $Wc.UseDefaultCredentials=$true 
            $Wc.DownloadFile($documentURL$destination) 
        } 
        else 
        { 
         Write-Host "Version Created: $versioncreated is out of range" -ForegroundColor Red 
        } 
      } 
    } 
 }
 How does it work?

 

Give it a date range to filter in the $startDate and $endDate variables, then set the site and library you would like to target.. Then click play (if in ISE).

 

Results

The script will send the documents to "C:\temp\downloads" so make sure this folder exists before running the script. At a later date I will be adding the feature to create the folder in the location.

Additionally the script will output its findings to the window:

 

Additional

A user in the forums asked if they could filter it out to download the latest published version

PowerShell
Edit|Remove
foreach($item in $items) 
 { 
 
    $version = $item.Versions.GetVersionFromID($item.Versions[1].VersionId) 
 
    if($version -ne $null) 
    { 
        Write-Host "" 
        Write-Host "Item Name:" $item.Name -ForegroundColor Yellow 
        Write-Host "Item version found" -ForegroundColor Green -NoNewline ;Write-Host " Version Created: $versioncreated" -ForegroundColor Cyan 
             
        $destination = "C:\temp\Downloads" + "\" + $version.VersionLabel + " " + $version.ListItem.Name 
        $documentURL = $web.Url + "/" + $version.url 
 
        $Wc = New-Object System.Net.WebClient 
        $Wc.UseDefaultCredentials=$true 
 
        $Wc.DownloadFile($documentURL$destination) 
    } 
 }
 We can achieve this by using the second item in the index of version, then use that to get the items ID.