SharePoint Online: Add Attachment to List Item using PowerShell

Requirement: Add Attachment to SharePoint Online List Item

SharePoint Online: How to Add Attachment to List Item?

Attachments are a great way to keep related documents and files together with your list items in SharePoint Online. In this post, we’ll walk you through the process of adding an attachment to a SharePoint Online list item.

To add an attachment to a list item in SharePoint Online, follow these steps:

  • Navigate to the SharePoint Online list >> Select the List Item you want to add attachments
  • Edit List Item >> Scroll down and Click on “Add Attachments” link in the Edit pane.
    sharepoint online add attachment to list item powershell
  • This opens browse dialog window., Select attachments to add
  • Click on Save to complete adding attachment to list item.
SharePoint Online list item attachment size limit: The file size limit on SharePoint Online list attachments is 250 MB!

PowerShell to Add an Attachment to List Item:

If you want to automate the process of attaching files to a list, PowerShell can make the process easy. Let’s walk you through how to attach a file to a specific list item in your SharePoint Online list using PowerShell. This is a handy way to attach multiple files to list items without having to open the item editor.

Here is the PowerShell script to add attachments to the list item:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Add-AttachmentToListItem()
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ItemID,
        [Parameter(Mandatory=$false)] [string] $AttachmentPath
    Try {
        #Setup Credentials to connect
        $Cred = Get-Credential
        $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Get the List & List Item
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItem = $List.GetItemByID($ItemID)

        #Get All existing attachments
        $AttachmentFiles = $ListItem.AttachmentFiles

        #Check if attachment file name exists already
        $FileName = Split-Path $AttachmentPath -Leaf
        $AttachmentFile = $AttachmentFiles | where { ($_.FileName -eq $FileName) }
        If($AttachmentFile -eq $Null)
            #Get the Attachment file from local disk
            [Byte[]]$Bytes = [System.IO.File]::ReadAllBytes($AttachmentPath)
            $ContentStream = New-Object -TypeName System.IO.MemoryStream -ArgumentList @(,$Bytes)
            #Create Attachment object
            $AttachmentCreation = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation
            $AttachmentCreation.ContentStream = $ContentStream
            $AttachmentCreation.FileName = $FileName

            write-host  -f Green "Attachment Added to List Item!" 
            write-host -f Yellow "Attachment File Name Exists already!"
    Catch {
        write-host -f Red "Error Adding Attachment to List!" $_.Exception.Message

#Set Parameters
$SiteURL= ""

#Call the function to copy list items
Add-AttachmentToListItem -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID -AttachmentPath $AttachmentPath

PnP PowerShell to Add Attachment to List Item

Let’s add an attachment to the SharePoint Online list item with PnP PowerShell.

#Set Variables
$SiteURL = ""
$ListName = "Projects"
$ItemID ="2"
$FilePath ="C:\Docs\UserInfo.csv"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the List Item
$Item  = Get-PnPListItem -List $ListName -Id $ItemID

#Get the File and Add to List Item Attachment
$FileStream = New-Object IO.FileStream($FilePath,[System.IO.FileMode]::Open) 
$AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation 
$AttachmentInfo.FileName =  Split-Path $FilePath -Leaf 
$AttachmentInfo.ContentStream = $FileStream
$AttchedFile = $Item.AttachmentFiles.Add($AttachmentInfo) 

How about adding multiple attachments to the list item? Let’s add all files from a given folder to SharePoint Online.

#Set Variables
$SiteURL = ""
$ListName = "Projects"
$ItemID ="2"
$FolderPath = "C:\Temp\Docs"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the List Item
$ListItem  = Get-PnPListItem -List $ListName -Id $ItemID

#Function to Add all files as a attachment to list item
Function Add-AttachmentsFromFolder($ListItem, $FolderPath)
    #Get All existing attachments from list item
    $AttachmentFiles = Get-PnPProperty -ClientObject $ListItem -Property "AttachmentFiles"

    #Get the File and Add to List Item Attachment
    ForEach ($File in  (Get-ChildItem $FolderPath -File))
        $AttachmentFile = $AttachmentFiles | Where { ($_.FileName -eq $File.Name) }
        If($AttachmentFile -eq $Null)
            $AttachmentInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation 
            $AttachmentInfo.FileName =  $File.Name
            $AttachmentInfo.ContentStream = $File.OpenRead()
            $AttchedFile = $ListItem.AttachmentFiles.Add($AttachmentInfo) 
            Write-host -f Green "Added Attachment File:"$File.FullName
            write-host -f Yellow "Attachment '$($File.Name)' Exists already!"
#Call the function to add all attachments from folder
Add-AttachmentsFromFolder $ListItem $FolderPath

My other post on downloading attachments from SharePoint Online list: How to Download Attachments from SharePoint Online List using PowerShell?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

4 thoughts on “SharePoint Online: Add Attachment to List Item using PowerShell

  • I have not been able to add any files with the third script, error:

    Cannot convert the argument “parameters”, with the value:
    “Microsoft.SharePoint.Client.AttachmentCreationInformation”, for “Add”, to type
    “Microsoft.SharePoint.Client.AttachmentCreationInformation”: “Cannot convert value
    Type “Microsoft.SharePoint.Client.AttachmentCreationInformation”
    “Microsoft.SharePoint.Client.AttachmentCreationInformation” to type
    “Microsoft.SharePoint.Client.AttachmentCreationInformation”. ”

    can u help me?

    • Just restart the PowerShell ISE – That solves the problem!

  • Kudos – this worked almost exactly as I wanted. In my case I wanted my AttachmentPath to reference an online document, which did not work. It only worked when my documents was local. Other than that, it worked perfectly.

  • I am getting error on 49thline.

    Exception calling “ExecuteQuery” with “0” argument(s): “parameters
    Parameter name: Specified value is not supported for the parameters parameter.”
    At line:36 char:13
    + $Ctx.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~

    If I remove the line, script has been ran successfully.
    But File has not been uploaded.Please help.


Leave a Reply

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