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?

1 Answer 1

$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)          


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, 2014 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, 2014 at 14:41
  • Try removing check in and check out code statements Nov 21, 2014 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, 2014 at 15:17
  • 1
    Ok, sorry you need to remove the checkout and checkin code. I have updated the code. Try now Nov 24, 2014 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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