Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Add Attachment to List Item in SharePoint using PowerShell

$
0
0
PowerShell script to add attachment to list item programmatically in SharePoint:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Custom Function to Add attachment to SharePoint List Item
Function Add-Attachment($Item, $AttachmentPath)
{
$FileContent = [System.IO.File]::ReadAllBytes($AttachmentPath)
$Item.Attachments.Add([System.IO.Path]::GetFileName($AttachmentPath), $FileContent)
$Item.Update()

Write-host "Attachment Added to List Item Successfully!"
}

#Variables
$SiteURL="https://portal.crescent.com/sites/Deals"
$ListName="Tasks"
$ItemID=1
$AttachmentPath="c:\Scripts\ASI-LOG.docx"

$web = Get-SPWeb $SiteURL
$List = $web.Lists[$ListName]
$Item = $list.GetItemById($ItemID)

#Call the function to Add Attachment
Add-Attachment $Item $AttachmentPath

Viewing all articles
Browse latest Browse all 1058

Trending Articles