Tuesday, February 26, 2008
« Sharepoint and Powershell Virtual lab | Main | PowerShell-SharePoint: Add a list item »

Deleting a list item with PowerShell is pretty easy also

If you know or can find out the ID of the list item use the GetItemById method of the list itself

 

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 

# Delete an item from the announcements list by Item ID

$site    =     new-object Microsoft.SharePoint.SPSite("http://teams.contoso.com")                                                        
$web     =    $site.rootweb                                                                                                

$list    =    $web.Lists["Announcements"]                                                                              

$deaditem=    $list.GetItemById(2)
$deaditem.Delete()

$web.Dispose()
$site.Dispose()                                                                                                      
 
Another way to delete items is to run a CAML query and delete items from the collection
This code sample deletes all those announcements that have expired dates before yesterday
 

# Delete an item from the announcements list by using a CAML query [void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $site = new-object Microsoft.SharePoint.SPSite("http://teams.contoso.com") $web = $site.rootweb $list = $web.Lists["Announcements"]

$DeleteBeforeDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([DateTime]::Now.AddDays(-1)) $caml=' <Where> <Lt> <FieldRef Name="Expires" /> <Value Type="DateTime">{0}</Value> </Lt> </Where> ' -f $DeleteBeforeDate $query=new-object Microsoft.SharePoint.SPQuery $query.Query=$caml $col=$list.GetItems($query) # Pipe results to a loop and delete each element $col | % { $_.Delete() } $web.Dispose() $site.Dispose()

 

Be very careful with this as a badly formed CAML query will return ALL the items in a list thus deleting all items in the list, use a line like $col | select Title instead of the delete to double check the results before you run the function. Using the invaluable CAML Builder utility from U2U for these queries is a good idea, just remember to remove the <Query> tags from the result.

The third way is to use some of Powershell's powerful string matching feature. This article gives a simple overview of the like and match commands in Powershell

Using like or match we could compare any field in a list and delete on that basis.

For instance lets say we need to delete all Announcements that had CEO in the title. This sample does that

 

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 

# Delete announcements that match a string pattern

$site    =     new-object Microsoft.SharePoint.SPSite("http://teams.contoso.com")                                                        
$web     =    $site.rootweb                                                                                                

$list    =    $web.Lists["Announcements"]                                                                              

$list.get_items() | where { $_.Title -like '*CEO*' } | % { $_.Delete() }
                                                                                              

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

 

Note the get_items() syntax as PowerShell has a problem with Items as a property so we have to use the assessor directly.

Tuesday, February 26, 2008 11:43:55 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |