6

I have 300 files in my document library and I need to delete one of them, I cant use the delete out of box option and I though I can use PowerShell to force it. Can anyone have any idea about this:

this one delete all files in library:

$web = get-spweb ""
$doclibsname = ""
$list = $web.lists | where {$_.title -eq $DocLibsname}
$items = $list.items
foreach ($items in $items)
{
$list.getitembyid($item.id).delete()
}
5
  • which item you want to delete? Commented Oct 15, 2015 at 6:28
  • 1
    It's because there is the Foreach instruction. How do you know the file you want to delete? do you have its id, or title name? If you have one of this element, you can change the line with: $item = $items.GetItemsById() and $item.Delete().
    – Seb
    Commented Oct 15, 2015 at 6:30
  • You need to add an if statement into your powershell to identify only the item you want to delete by title or some other attribute first.
    – Erik Perez
    Commented Oct 15, 2015 at 6:32
  • No response found from you! :(. did my trick work for you? Commented Oct 15, 2015 at 8:47
  • 1
    There's a typo in the foreach. Change ($items in $items) to ($item in $items)
    – iOnline247
    Commented Oct 19, 2015 at 20:11

1 Answer 1

8

You have to find your item at first based on a condition. Like

If($item.ID -eq 10)
{
$item.delete()
}

OR

If($item["Some Column Name"] -eq "Some Value")
{
$item.delete()
}

If you know the $item ID already, then it is very easy to delete

$list.getitembyid("ID of your Item").delete()

There are other operators you may use in If statement like

-ne : not equal,

-gt : greater than,

-ge : greater than or equal to,

-lt : less than,

-le : less than or equal to,

-like wildcard comparison,

-notlike : wildcard comparison,

-match : regular expression comparison

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.