PowerShell Add Attachment to Item

Someone asked me how they could automatically add attachments to items. What I did was use a workflow action (ilovesharepoint) to call a powershell script. The script will grab a file from a document library and add it to the list item.

$web = Get-SPWeb "http://taco.com/subsite/"
$filePath = "http://taco.com/subsite/docs/grandetaco.docx"

$spList = $web.lists["Toppings"] 
$item = $splist.GetItemById(2)
$file = $web.GetFile($filePath).OpenBinary()

$item.Attachments.Add($filepath, $file)
$item.Update()

$web.Dispose()

live and learn!
i was having a hard time converting the file to bytes to be able to attach the document to the item.

Leave a Reply

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