Showing posts with label SharePoint List. Show all posts
Showing posts with label SharePoint List. Show all posts

Saturday, October 5, 2013

Delete All SharePoint List Items Using PowerShell 2010

How to delete all list items from SharePoint list?

Follow the steps to delete all list items from SharePoint List

1. Create new PowerShell (.ps1) file with below script using Notepad

$Url = "http://site-url:5000"
$ListName = "Sales 2013"
$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()
Write-Host "Total Items to be deleted : $($Items.count)"

if($Items.count -gt 0)
{
  $shell = new-object -comobject wscript.shell
  $result = $shell.popup("Total Item to be deleted : $($Items.count) `nDo   you want to continue?",0,"Alert",4+32)

if($result -eq "6")
{
foreach ($item in $Items)
{
  $itemId = $item.ID
  $List.GetItemById($itemId).Delete()
  Write-Host "Deleted list item with id $($itemId)"
}
}
}
$List.Update()
$Web.Dispose()


2. Replace values of parameters $Url and $ListName

3. Save file in server hard drive (Example: D:\PowerShell\Delete-List-Items.ps1)

4. Open "SharePoint 2010 Management Shell" with "Run as administrator"

5. Navigate to the folder where the script  file is stored
    (Example: cd D:\PowerShell)

6. Select file to be executed
    (Example: .\Delete-List-Items.ps1)

7. Press enter and enjoy!!

Tuesday, March 19, 2013

Create new SharePoint List Item using SharePoint 2010 Management Shell (PowerShell)

Follow the steps to create new SharePoint List Item using SharePoint 2010 Management Shell (PowerShell):
1.       Open SharePoint 2010 Management Shell with Run as Administrator

2.       Modify and execute following script:

#Web absolute URL where list exists
$webURL = "<http://site:port/site/subsite>" 

#SPList Name where items to be added
$listName = "<List Name>" 

#Get the SPWeb object and save it to a variable
$web = Get-SPWeb $webURL 

#Get the SPList object to retrieve the List
$list = $web.Lists[$listName] 

#Create a new item
$newItem = $list.Items.Add() 

#Add properties to this list item
$newItem["Title"] = "Title Text"
$newItem["Description"] = "Description Text" 

#Update the object so it gets saved to the list
$newItem.Update()

$web.Dispose()

Contact Me for any technical help related with SharePoint 2010.