Friday, September 16, 2016

How to get attachments from SharePoint list with PowerShell?

SharePoint 2013 it works

$webUrl = "http://peakfinder/"    
$library = "Product 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://peakfinder/" + $file.ServerRelativeUrl
        Write-Host "http://peakfinder/"$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

No comments:

Post a Comment