0

I have a document library and versioning is enabled. I would like to to restore a previous minor version by powershell. Is this possible? I know it is possible with major versions with the script below:

$web = Get-SPWeb -Identity http://sp2010
$list = $web.Lists["Documents"]
$items = $list.Items
foreach ($item in $items)
{
    $file = $item.File
    $fileVersions = $file.Versions      
    if ($fileVersions.Count -gt 0)
    {
        $file.CheckOut()
        $fileVersions.Restore($fileVersions.Count - 1)
        $file.CheckIn("Checkin comment")
    }   
}

UPDATE!

I tested to restore manual to a minor version. It is existing and I have webapplication full control permissions with my account. In the GUI it is possible to restore to minor versions with my account. The results of script below, creates a new version. That OK and no problem. But the metadata is is not what I am expecting. The metadata is unchanged. It needs to change to the metadata of version 0.1! What is going wrong?

#Add SharePoint PowerShell SnapIn if not already added 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { 
    Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
} 

write-host "Start" -ForeGroundColor magenta

$web = Get-SPWeb -Identity https://portal.myCompany.net/projects/25010095/
$list = $web.Lists["test"]

if($list -ne $null)
{
    $items = $list.Items
    foreach ($item in $items)
    {
        # just a test on 1 document
        if($item.ID -eq "1274")
        {
            $file = $item.File
            $fileVersions = $file.Versions      
            if ($fileVersions.Count -gt 0)
            {
                #$currentVersion = $fileVersions.Count
                #$newVersion = $fileVersions.Count - 1

                #foreach($fv in $fileVersions)
                #{
                #    $fv.Properties["vti_sourcecontrolversion"]
                #    $fv.Properties["vti_oldversion"] 
                #}

                #write-host "from version " $currentVersion " to " $newVersion " : " $item.Name -ForeGroundColor magenta
                $file.CheckOut()
                $fileVersions.Restore(0.1)
                $file.CheckIn("restore to previous version by script")
            }
            else
            {
            }  
        } 
    }
}
else
{
    write-host "List cannot be found" -ForeGroundColor magenta
}

write-host "Finish" -ForeGroundColor magenta
  • As mentioned in your previous question, restoring creates a new version. so, if you go to UI and try to restore 0.1 a new version 0.3 will be created which will be similar to 0.1. And this script does restore minor versions as well, if the user running it has access to minor versions. – Nadeem Yousuf Nov 24 '14 at 12:02
  • I know the version will be increased by this script. Thats no problem. The script above is get Versions.Count and thats not OK! If I have 0.1, 0.2 and 0.3. The script needs to calculate (0.3 - 1 = 0.2). After this calculation I need to restore to version 0.2. See also a test script where I try manual restore to minor version, it is not working! – Ola Nov 24 '14 at 12:07
  • I updated the original question – Nadeem Yousuf Nov 24 '14 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.