Skip to main content
1 of 5
user3426869
  • 173
  • 2
  • 5
  • 15

Get attachments from sharepoint list with powershell

I made this script :

$webUrl = "http://site.fr/"    
$library = "Qualified Information"
$spSite = new-object Microsoft.SharePoint.SPSite($webUrl)
$w = $spSite.OpenWeb()
$l = $w.Lists[$library]
$resultHashtable = @{}

foreach ($listItem in $l.Items)
{
 
    Write-Host "    Content: " $listItem.ID 
    foreach ($attachment in $listItem.Attachments)
    {
        $file = $w.GetFile($listItem.Attachments.UrlPrefix + $attachment)
        $linkAttachment = "http://site.fr" + $file.ServerRelativeUrl
        Write-Host "http://site.fr"$file.ServerRelativeUrl
        if($linkAttachment){
        $resultHashtable.Add($listItem.ID, $linkAttachment)
        }

    }

}
#Export CSV
$resultHashtable.GetEnumerator() | Sort-Object -Property Name -Descending |
Select-Object -Property @{n='SiteURL';e={$_.Name}},Value |
Export-Csv -Path Attachments.csv -NoTypeInformation

With a Sharepoint 2013 it works but with sharepoint 2010, my scripts doesn't the list :

Exception calling "GetFile" with "1" argument(s): "Value does not fall within the expected range."

If I debug "$l", it's null...

My "Write-Host " Content: " $listItem.ID " just return : "Content:" My list is full of items with attachments, do you have an idea?

Thanks.

user3426869
  • 173
  • 2
  • 5
  • 15