none
Powershell script to copy a list attachment to a file

    Question

  • I have a script that builds a list of items.  I'd like to add the ability to download the list items' attachments to a local directory.  IS this possible?  There are lots of examples of how to add attachments but nothing that can download the attachments.  Any help would be appreciated.
    Wednesday, September 1, 2010 8:06 PM

Answers

  • This should do the trick...

    [void][reflection.assembly]::Loadwithpartialname("microsoft.sharepoint")

    $webUrl = "<your weburl>"    # the url to the site that has the list
    $library = "<your library name>"   # Library Name
    $tempLocation = "C:\Temp\"     # Path to dump files
    $itemId = 1                    # Item Id to read attachments from

    # Load the Site / Web
    $s = new-object Microsoft.SharePoint.SPSite($webUrl)
    $w = $s.OpenWeb()

    # Grab the list and item that you want to download the attachments from
    $l = $w.Lists[$library]
    $i = $l.GetItemById($itemId)

    # Loop thru each attachment
    foreach ($attachment in $i.Attachments)
    {
        # Get the attachment
        $file = $w.GetFile($i.Attachments.UrlPrefix + $attachment)
        $bytes = $file.OpenBinary()
       
        # Build the destination path
        $path = $tempLocation + $attachment
       
        Write "Saving $path"
       
        # Download the file to the path
        [System.IO.FileStream] $fs = new-object System.IO.FileStream($path, "OpenOrCreate")
        $fs.Write($bytes, 0 , $bytes.Length)
        $fs.Close()
    }

     


    Eli Van Eenwyk
    Visit my blog @ http://www.inergex.com/blogs/sharepoint
    • Marked as answer by Grub00 Thursday, September 2, 2010 2:46 PM
    Thursday, September 2, 2010 2:41 PM

All replies

  • This should do the trick...

    [void][reflection.assembly]::Loadwithpartialname("microsoft.sharepoint")

    $webUrl = "<your weburl>"    # the url to the site that has the list
    $library = "<your library name>"   # Library Name
    $tempLocation = "C:\Temp\"     # Path to dump files
    $itemId = 1                    # Item Id to read attachments from

    # Load the Site / Web
    $s = new-object Microsoft.SharePoint.SPSite($webUrl)
    $w = $s.OpenWeb()

    # Grab the list and item that you want to download the attachments from
    $l = $w.Lists[$library]
    $i = $l.GetItemById($itemId)

    # Loop thru each attachment
    foreach ($attachment in $i.Attachments)
    {
        # Get the attachment
        $file = $w.GetFile($i.Attachments.UrlPrefix + $attachment)
        $bytes = $file.OpenBinary()
       
        # Build the destination path
        $path = $tempLocation + $attachment
       
        Write "Saving $path"
       
        # Download the file to the path
        [System.IO.FileStream] $fs = new-object System.IO.FileStream($path, "OpenOrCreate")
        $fs.Write($bytes, 0 , $bytes.Length)
        $fs.Close()
    }

     


    Eli Van Eenwyk
    Visit my blog @ http://www.inergex.com/blogs/sharepoint
    • Marked as answer by Grub00 Thursday, September 2, 2010 2:46 PM
    Thursday, September 2, 2010 2:41 PM
  • Brilliant!  It worked.  Thanks for taking the time to post it.  Also, your code is well documented and easy to read.  Thanks again...

     

     

    Thursday, September 2, 2010 2:53 PM
  • Good One. Works Great !!!!
    Thursday, October 17, 2013 1:39 PM
  • Eli

    I know this item is older , but I just came across it today and I was wondering if

    is there a way to make this not dependant on the $itemId = 1 

    or use the %Currentitem=ID%  call

    as my id numbers change with new items in the spslist.


    Steve Athey

    Monday, June 22, 2015 8:18 PM