Purging a SharePoint List using Powershell!

February 11th, 2009 § 1 Comment

Have you ever wanted a way to quickly purge a SharePoint List or Document Library? With some help from this blog post I was able to come up with a Powershell script that you can use to empty a SharePoint List very quickly. This example also deletes all items recursively keeping the folder structure intact.

Start-Transcript ‘c:\powershell\logs\transcript.txt’
[System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

$site = new-object Microsoft.SharePoint.SPSite(“http://localhost”)
$web = $site.rootweb
$list = $web.Lists["Name of your list that needs cleansing"]

$caml=’<Where><Eq><FieldRef Name=”ContentType” /><Value Type=”Text”>Item</Value></Eq></Where>’

$query=new-object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = “Scope=’Recursive’”
$query.Query=$caml  | Write-Output

$items=$list.GetItems($query)

# Pipe results to a loop and delete each element

$items | % { $_.Delete() }

$web.Dispose()
$site.Dispose()
Stop-Transcript

Much of the magic is in the CAML query as that is where you define what you’re looking to delete as well as if you want it to work recursively down the List structure or not. Cheers!

Advertisement

Tagged: , , , ,

§ One Response to Purging a SharePoint List using Powershell!

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Purging a SharePoint List using Powershell! at The SharePoint Swiss Army Knife.

meta

Follow

Get every new post delivered to your Inbox.

Join 584 other followers