I am using sp 2010. I have a document library. I would like to go to one previous version of all the documents in this document library. Maybe in powershell? Something like a foreach loop and for each document go to the previous version?

up vote 9 down vote accepted
$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)
    {           
        $fileVersions.Restore($fileVersions.Count - 1)          
    }   
}

UPDATE

Please note that when any of previous versions is restored a new version is created.

  • Great! I will try it and let you knwo if it is working – Ola Nov 21 '14 at 12:16
  • this script is not working. It creates a new version, but dont changes any metadata. What to do now? – Ola Nov 21 '14 at 14:41
  • Try removing check in and check out code statements – Nadeem Yousuf Nov 21 '14 at 15:11
  • I would like to go from version 0.2 to 0.1. That is not possible with your script because there is a check : if version is greater then 1 – Ola Nov 21 '14 at 15:17
  • 1
    Ok, sorry you need to remove the checkout and checkin code. I have updated the code. Try now – Nadeem Yousuf Nov 24 '14 at 12:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.