Play with SharePoint 2010 List using PowerShell

In this post, we will learn how to do basic operations on SharePoint list using PowerShell.

Lets start with List creation

$spAssigment = Start-SPAssignment    #Use to dispose SPWeb safely
$spWeb = Get-SPWeb ServerURL -AssignmentCollection $spAssignment     #Get SPWeb Instance
$spWeb.ListTemplates | Select Name, Description    #Display List of Templates Available
$spTemplate = $spWeb.ListTemplates["Custom List"]     #Create SPTemplate instance of Type Custom List
$spWeb.Lists.Add("List Title", "Description", $spTemplate)     #Add list to site

Add list to quick launch and fields to list

$spList = $spWeb.Lists["List Title"]    #Get list instance
$spList.OnQuickLaunch = $True    #Set true to display in Quick Launch
$spList.Update()    #Update list to reflect changes in site
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text      #Get Field type to create
$spList.Fields.Add("My Column", $spFieldType, $false)      #Add new field to list

Create View and set fields for view

$spViewQuery = “<OrderBy><FieldRef Name=”"Modified”" Ascending=”"False”" /></OrderBy>”
$spViewFields = New-Object System.Collections.Specialized.StringCollection    #Create string collection object
$spViewFields.Add("Title")    #Add columns
$spViewFields.Add("My Column")
$spViewName = "My View"        #Set view name
$spListView = $spList.Views.Add($spViewName, $spViewFields, $spViewQuery, 100, $True, $False, “HTML”, $False)    #Add view to list
$spListView.DefaultView = $True       #Set view as default
$spListView.Update()    #Update view to reflect changes in site

Add few List Items

$spListItem = $spList.Items.Add()    #Add new list item object
$spListItem["Title"] = "First Item"    #Set field value
$spListItem["My Column"] = "My Text for first item"
$spListItem.Update()    #Update list item object to reflect changes in site

$spListItem = $spList.Items.Add()
$spListItem["Title"] = "Second Item"
$spListItem["My Column"] = "My Text for second item"

Update List Items

$spListItems = $spList.Items    #Get list item collection
$spListItemsCount = $spListItems.Count    #Get list items total count
for($Counter = $spListItemsCount-1; $Counter -ge 0; $Counter--)
    $spListItem = $spListItems[$Counter]    #Get list item via index
    $spListItem["Title"] = "Updated Title" + $Counter
    $spListItem["My Column"] = "My Column" + $Counter

Delete List Items

for($Counter = $spListItemsCount-1; $Counter -ge 0; $Counter--)
    $spList.Items[$Counter].Delete()    #Delete list item

Delete List


Finally Dispose web object

Stop-SPAssignment $spAssignment

Happy Reading!!!


About Rajan
I am Rajan Garg working as a Senior Software Engineer in a company. I am working on the Microsoft technologies.

4 Responses to Play with SharePoint 2010 List using PowerShell

  1. Ben says:

    Good stuff, thx. I modeled a bunch of functions around this info.

  2. KRyan says:

    Really nice summary of list commands. Can you post something about how to count items in a list? Thanks!

    • Rajan says:

      Hello KRyan,

      Please check Update List Items section. Count property of list item collection will give you the items count in a list.


  3. test says:

    Can you tell me how to delete a custom list template using power shell

Leave a Reply

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

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

Google+ photo

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

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

%d bloggers like this: