SharePoint Online PowerShell delete list items

X

Privacy & Cookies

This site uses cookies. By continuing, you agree to their use. Learn more, including how to control cookies.

Got It!
Advertisements

Sometimes when Im working on something related to SharePoint, I come across scenarios where Im constantly building up test data in Lists. Frequently when doing this, I have a need to almost constantly delete and recreate list items.

Rather than using the UI or writing nice PowerShell one-liners [which for the record I love doing], I decided to throw together a little script for this.

This script takes two mandatory parameters, one for URL and one for List Name. Assuming you give it the correct values, it will quickly use Object Model code to call the Delete[] method against every item in the list [or library].

Here is the script, hope it works for you! Note: I did not add any kind of confirmation, so if you run this it WILL literally delete all items, it wont first make sure youre serious about it so make sure you know what youre doing!

RD

.\Delete-SPListItems.ps1 -Url //intranet -ListName "Test List" This example deletes list items from a list called Test List in the //intranet web. .Notes Name: Delete-SPListItems Author: Ryan Dennis Last Edit: 8/2/2012 Keywords: Delete List Items .Link //www.sharepointryan.com .Inputs None .Outputs None #Requires -Version 2.0 #> [CmdletBinding[]] Param[ [Parameter[Mandatory=$true]][System.String]$Url, [Parameter[Mandatory=$true]][System.String]$ListName ] $Web = Get-SPWeb $Url $List = $Web.lists[$ListName] if[$List -eq $null]{ Write-Error "The List cannot be found";return } Write-Warning "Deleting all list items from $[$ListName]" $Items = $List.GetItems[] $Items | ForEach-Object{ $List.GetItemById[$_.Id].Delete[] } $List.Update[] $Web.Dispose[]
Advertisements

Share:

Related

  • Return SPListItems using CSOM and PowerShell without writing CAML
  • March 7, 2014
  • In "CSOM"
  • Preserve Modified By values while editing List Items using PowerShell
  • December 2, 2011
  • In "Administration"
  • Making SharePoint 2010 PowerShell Scripts Backward-Compatible with 2007
  • November 4, 2011
  • In "Administration"

Video liên quan

Chủ Đề