5

I am trying to delete all items from my Document Library using PowerShell using the below code. But its not deleting the checked out items from the Document Library.

How to delete the checkedout items along with checkedin items from document library.

Add-PSSnapin -Name Microsoft.Sharepoint.Powershell
write-host "continuing" 
$web = get-SPWeb "http://sirvr:123/sites/OnM/"
$DocLibsName = "CBM-SE"
$list = $web.lists | where { $_.title -eq $DocLibsName }
Write-host "List $($list.title) has $($list.items.count) entries"
$items = $list.items
foreach ($item in $items)
{
    $item.CheckIn("Checked In By Administrator")
    #To publish a file:
    $item.Publish("Automatically published by Powershell")
    #To approve a file:
    $item.Approve("Automatically approved by by Powershell");
    Write-host " item is  approved..."
    $list.getitembyid($Item.id).Delete()
    Write-host " item is deleted successfully..."
}
6

You can check-in all checked out documents and then delete

$web = Get-SPWeb http://weburl
$folder = $web.GetFolder("Library Name")
$folder.Files | Where { $_.CheckOutStatus -ne "None" } | ForEach
{
    $_.CheckIn("Checked in")        
}
$web.Dispose()

Update

You can fill in a temporary value to those fields. Then do check-in. Then delete

$web = Get-SPWeb http://siteurl
$folder = $web.GetFolder("Library Name")
$files = $folder.Files
foreach($file in $files)
{
    $file.Item["FileName"] = "Temporary Value";
    $file.Item.Update();        
}
$web.Dispose()
  • Thanks for your line of code. There exists an issue here, assuming there are mandatory columns which needs to be filled in the upload document page. I have uploaded the file using the PowerShell without filling the mandatory columns. Now, when I try to delete these items from document library I am not able to delete these items. – SaMolPP May 6 '15 at 10:46
  • @SaMolPPp please see my updated answer. – Amal Hashim May 6 '15 at 12:12

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.