Scripting inside SharePoint with PowerShell - PART II Playing with lists and items...
By Michael on Friday 31 December 2010, 17:05 - MOSS 2010 - Permalink
This sample code show how you can script easily SP lists and items, this
example lists all lists items for sites and sub sites and write the result into
a text file:
[System.Reflection.Assembly]::LoadWithPartialName("Microsodt.SharePoint")
$logfolder = "c:\Logs"
$Siteurl = "http://sp.com"
$mysite = new-object Microsoft.SharePoint.SPSite($Siteurl)
$Filename "Report.txt"
$LogDate = get-date -uformat "%Y/%m/%d"
new-item -path $logfolder -name $Filename - type file -force
add-content -path $logfolder\$Filename -value "Date ; URL ; ListName ; ID
; Title ; Name"
$rootweb = $mysite.RootWeb
Foreach ($web in $mysite.Allwebs)
{
$lists = $web.Lits
foreach ($list in $lists)
{
$txt = "List Name:" + $list.Title
write-output $txt
$items = $list.Items
foreach ($listitem in $items)
{
$txt = $LogDate + ";" + $web.Url + ";" + $list.Title + ";" + $listitem.ID
+ ";" + $listitem.Title + ";" $listitem.Name
write-output $txt
add-content -path $logfolder\$Filename -value $txt
}
}
}
In few words, In the first loop we taking each web site and in the second
loop we list all lists for this web site and in the third loop we taking
specific attributes for each items.
In the next article we will see much operation and automation scripts for
SP 2010 such as backup, restore, manage account, etc.