Get
ListItem Attachments using Iterating Large SharePoint Lists with PowerShell.
$SPAssignment = Start-SPAssignment
$webUrl
= "http://sharepointurl:1234/"
$listname
= "ListDisplayName"
$destinationfolder
= "c:\backupfiles"
$site
= new-object
Microsoft.SharePoint.SPSite($webUrl)
$web
= $site.OpenWeb()
$list
= $web.Lists[$listname]
$spQuery
= New-Object
Microsoft.SharePoint.SPQuery
$query
= '<Where><IsNotNull><FieldRef
Name="ID" /></IsNotNull></Where>'
$spQuery.ViewAttributes
= "Scope = 'Recursive'"
$spQuery.Query
= $query
$spQuery.RowLimit
= 2000
Write-Host
"
ItemCount: " $list.ItemCount
do
{
$spListItemCol = $list.GetItems($spQuery)
$spQuery.ListItemCollectionPosition =
$spListItemCol.ListItemCollectionPosition
foreach ($listItem in $spListItemCol)
{
Write-Host " Item ID:
" $listItem.ID
if (!(Test-Path -path $destinationfolder))
{
$dest = New-Item
$destinationfolder -type directory
}
foreach ($attachment in $listItem.Attachments)
{
$file =
$web.GetFile($listItem.Attachments.UrlPrefix + $attachment)
$bytes =
$file.OpenBinary()
$path = $destinationfolder
+ "\" + $listItem.ID + "_" + $attachment
Write "Saving $path"
$fs = new-object
System.IO.FileStream($path, "OpenOrCreate")
$fs.Write($bytes, 0 ,
$bytes.Length)
$fs.Close()
}
}
}
while
($spQuery.ListItemCollectionPosition -ne $null)
Stop-SPAssignment $SPAssignment
Stop-SPAssignment $SPAssignment