0

I have a SharePoint document library with a large number of documents and versions. My task is to create a PowerShell script to delete all versions but the current for each document.

When I tried to delete the versions within my iteration, I got an error saying that the "collection was modified". Fair enough, I can't change the collection I'm looping through.

So instead I added the versions to be deleted in an array to be processed later outside the loop. But that didn't work eighter, I get "Exception calling Delete with 0 argument(s): Object reference not set to an instance of an object."

I have reviewed all similar Q&As for this issue on this site, but no answer is spot on. Can anyone give a hint on what is going on here? Thanks so much for any help.

asnp *sh*
$site = new-object Microsoft.SharePoint.SPSite("http://somesite")
$web = $site.RootWeb
$list = $web.Lists["Dokument"]
cls
$versionsToDelete = @()

foreach ($listItem in $list.Items)
{
    foreach ($ver in $listItem.Versions)  
    {
        if (!$ver.IsCurrentVersion)
        {
            $versionsToDelete += $listItem.Versions.GetVersionFromLabel($ver.VersionLabel)
        }
    }
}

for($i=0;$i -lt $versionsToDelete.Count;$i++)
{
    write-host "Trying to delete" $versionsToDelete.VersionLabel[$i]
    $versionsToDelete[$i].Delete()
}

$web.Dispose()
$site.Dispose()

2 Answers 2

0

You could refer to the below code:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

$SPweb = Get-SPWeb "http://sp13/sites/team"
$limit = (Get-Date).AddMonths(-6)
$SPlist = $SPweb.Lists["Doc1"]

foreach ($SPitem in $SPlist.Items)
{
    $currentVersionsCount= $SPItem.Versions.count

    for($i=$currentVersionsCount-1;$i -ge 0;$i--)
    {
        if($SPitem.Versions[$i].IsCurrentVersion){
            Write-Host "Current version"
        }
        else{ 
            if($limit -gt $SPitem.Versions[$i].Created)
            {
                $SPitem.Versions[$i].delete();
            }
        }
    }
}

OR:

###########################################################################  
#### Delete-Item-Versions-in-SharePoint using PowerShell - Report Mode ####  
###########################################################################  
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null)   
{  
    Add-PSSnapin Microsoft.SharePoint.PowerShell;  
}  
  
$myWeb = "http://mysharepoint.com/sites/myweb1"  
$myList = "Fantastic Library"  
$myDate = "2014-01-01"  
$myPath = "E:\Satyajit\VersionHistoryDeleteCheckWithDateFilter.csv"  
  
$results = @()  
$storageSaved = 0  
  
$SPWeb = Get-SPWeb $myWeb  
foreach ($list in $SPWeb.Lists)  
{  
    if ($list.Title -eq $myList)  
    {  
        Write-Host "Name: " $list.Title  
        Write-Host "Type: " $list.BaseType  
        $itemColl = $list.Items  
        foreach ($item in $itemColl)  
        {             
            foreach ($ver in $item.Versions)  
            {  
                #this block for non-current versions  
                if (!$ver.IsCurrentVersion)          
                {   
                  if ((Get-Date $ver.Created) -lt (Get-Date $myDate))  
                  {                                        
                        $RowDetails = @{  
                            "Document Name" = $item.Name  
                            "Version Number" = $ver.VersionLabel  
                            "Created Date" = $ver.Created  
                            "Created By" = $ver.CreatedBy  
                            "Delete Flag" = "Can be deleted"   
                            "Date Flag" = "Match"      
"Version Size (Kb)" =  "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                       
                        }  
                        $tempSize = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1MB)  
                        $storageSaved = $storageSaved + $tempSize    
                        $results += New-Object PSObject -Property $RowDetails   
                   }  
                   else  
                   {  
                        $RowDetails = @{  
                            "Document Name" = $item.Name  
                            "Version Number" = $ver.VersionLabel  
                            "Created Date" = $ver.Created  
                            "Created By" = $ver.CreatedBy  
                            "Delete Flag" = "Do not Delete"   
                            "Date Flag" = "Does not Match"   
                "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                          
                         }  
                         $results += New-Object PSObject -Property $RowDetails                           
                   }  
                }  
                 
                #this block for current versions  
                else  
                {  
                    if ((Get-Date $ver.Created) -lt (Get-Date $myDate))  
                    {    
                        $RowDetails = @{  
                                "Document Name" = $item.Name  
                                "Version Number" = $ver.VersionLabel  
                                "Created Date" = $ver.Created  
                                "Created By" = $ver.CreatedBy       
                                "Delete Flag" = "Do not Delete "   
                                "Date Flag" = "Match"      
                "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)              
                            }  
                            $results += New-Object PSObject -Property $RowDetails   
                    }  
                    else  
                    {  
                        $RowDetails = @{  
                                "Document Name" = $item.Name  
                                "Version Number" = $ver.VersionLabel  
                                "Created Date" = $ver.Created  
                                "Created By" = $ver.CreatedBy     
                                "Delete Flag" = "Do not Delete"   
                                "Date Flag" = "Does not Match"    
        "Version Size (Kb)" = "{0:n2}"-f ($item.File.Versions.GetVersionFromLabel($ver.VersionLabel).Size/1KB)                                      
                            }  
                         $results += New-Object PSObject -Property $RowDetails                          
                    }  
                }  
                  
            }  
              
        }  
    }      
}  
$TotalSizeSaved = @{  
    "Document Name" = ""  
    "Version Number" = ""  
    "Created Date" = ""  
    "Created By" = ""    
    "Delete Flag" = ""  
    "Date Flag" = ""  
    "Version Size (Kb)" =  "Total Size = " + $storageSaved   
}  
$results += New-Object PSObject -Property $TotalSizeSaved  
$results | export-csv -Path $myPath -NoTypeInformation  
Write-Host "---------------------------------------------------------------"  
Write-Host $storageSaved " MB can be saved from this library"  
Write-Host "---------------------------------------------------------------" 

References:

Powershell script for deleting old versions Delete Item Versions In SharePoint Programmatically

0

If you are comfortable running PowerShell scripts then the above script will do.

If running PowerShell is outside your comfort zone then you can also use browser-based tools (e.g. here)

Paul

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.