I'm using the following script to try and delete items from a Doc Library rather unsuccessfully. There are no files in the root, just all in subfolders under this doc library. Where am I going wrong on getting it to search the folders and remove from there?

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$urlSite = “”    
$listname = “Documents”    
write-host "Opening site..."    
$web=get-spweb $urlSite    
write-host "Opening list..."    
$list=$web.lists | ? { $_.title -eq $listName}    
$spQuery= New-Object Microsoft.SharePoint.SPQuery    
$spQuery.RowLimit = 200    
$caml = "<OrderBy><FieldRef Name='ID' Ascending='False'/></OrderBy>"    
    $listItems = $list.GetItems($spQuery)    
    $spQuery.ListItemCollectionPosition= $listItems.ListItemCollecitonPosition    
    $batch="<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>"    
    for($j=0;$j -lt $count; $j++)    
        $command+="<SetList Scope='Request'>$($list.ID)</SetList>"    
        $command+="<SetVar Name='ID'>$($item.ID)</SetVar>"    
        $command+="<SetVar Name='owsfileref'>$($web.Url)/$($item.Url)</SetVar>"    
        $command+="<SetVar Name='Cmd'>Delete</SetVar>"    
        if($i -ge $count){break}
    write-host "Sending batch..."   
    write-host "Emptying Recycle Bin..."    
while($spQuery.ListItemCollectionPosition -ne $null)
write-host "Process finished"

I need to get 100,000 items odd items deleted and it seems like this is the only method but I can't quite get it working.



Summary: When running ProcessBatchUpdate on a document library you need to pass the server relative url of the file in each item. Use SPListItem.File.ServerRelativeUrl or SPListItem.Folder.ServerRelativeUrl.

Change the following line...

$command+="<SetVar Name='owsfileref'>$($web.Url)/$($item.Url)</SetVar>"


$command+="<SetVar Name='owsfileref'>$item.File.ServerRelativeUrl</SetVar>"
| improve this answer | |
  • This doesn't seem to recurse the folder structure in the document library still. – PipBoy Jan 13 '15 at 9:49
  • 1
    Try use...$spQuery.ViewAttributes="Scope='Recursive'" – Avijit Sur Jan 13 '15 at 10:08
  • Same effect, doesn't recurse the folder structures. – PipBoy Jan 13 '15 at 10:10

Your Answer

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

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