Add Attachment to List Item in SharePoint using PowerShell

How to Add an Attachment to a SharePoint List Item?

In SharePoint, you can add attachments to list items to provide more information or to supplement the data in the list. The attached files can be documents, images, or other types of files. In this article, we will show you how to add attachments to list items.

To add an attachment to a SharePoint list item, do the following:

  • Navigate to the list, Open the list item you wish to add attachment
  • Click on the “Attach File” button from the “Items” tab of the ribbon. 
  • Browse and select the file to attach. Click OK to attach the file to the item. You can also edit the list item and then attach the file.
    powershell add attachment to sharepoint list item

PowerShell to add an attachment to SharePoint list item

Here is the PowerShell script to add an attachment to list items 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

To add an attachment to a SharePoint Online list item, use: SharePoint Online: Add Attachment to list item using PowerShell

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *