fbpx

Company News

PowerShell to Grab a Collection of Files from a Document Library

January 19, 2014

The following PowerShell can be used to take a subset of files from a document library in SharePoint and output them to the file system.  Note, this will need to be executed on the SharePoint server.

$spweb = get-spweb -identity "http://yourdomain/yourweb"

# Use the "pretty name" of your library below
$list = $spweb.lists["Your Library"]

# Below is an example of matching an attribute (Type) with a wildcard (*Queen*)
$listItemsLikeQueen = $list.Items | ?{$_["Type"] -like "*Queen*"}

# Below is an example of matching based on a Content Type 
$listItems = $listItemsLikeQueen | ?{$_.ContentType.Name -eq "Monarchy"}
$Destination = "D:\Out"

# Use the name of your library as you named it at the time of creation below (we all remove spaces when we create libraries, right?)
$DocumentLibrary = "YourLibraryWithoutSpaces"

# For each item, output it to the destination path above
foreach ($listItem in $listItems)
{
    # Change the output path of the list item, and swap around the slashes
    $DestinationPath = $listItem.Url.replace("$DocumentLibrary","$Destination").Replace("/","")
    write-host "Downloading $($listItem.Name) -> $DestinationPath"

    if (!(Test-Path -path $(Split-Path $DestinationPath -parent)))
    {
        write-host "Creating $(Split-Path $DestinationPath -parent)"
        $dest = New-Item $(Split-Path $DestinationPath -parent) -type directory
    }
    $binary = $spWeb.GetFile($listItem.Url).OpenBinary()
    $stream = New-Object System.IO.FileStream($DestinationPath), Create
    $writer = New-Object System.IO.BinaryWriter($stream)
    $writer.write($binary)
    $writer.Close()
}

#Clean up
$spWeb.Dispose()

The inspiration for this came from the following forum post

http://social.msdn.microsoft.com/Forums/sharepoint/en-US/75be246c-1ef4-4223-9531-ec87c1612e54/copy-a-file-from-sharepoint-2010-to-file-share-with-powershell?forum=sharepointcustomization

As well as this blog post (the blog post below shows you how to take ALL the files in a library and output them to a local path).

http://www.jbmurphy.com/2012/04/25/powershell-script-to-copy-a-sharepoint-document-library-to-a-local-path/

And some ideas of how to filter items in the list using PowerShell can be found below.

http://www.thorntontechnical.com/tech/sharepoint/quick-tip-sharepoint-powershell-get-items-in-a-list-based-on-custom-columns-and-other-hints

3 Responses

  1. RedK says:

    It’s not working for me. The script is not giving any erros, but its not downloading any files in the location I specified.

  2. RedK says:

    I think I don’t understand the ‘# Change the output path of the list item, and swap around the slashes’ part. What should I do there? I already specified the document library and the location where do download to. So what should I do here then?

  3. RedK says:

    Figured it out already. Had to change this:
    foreach ($listItem in $listItems)
    to this:
    foreach ($listItem in $listItems Like Queen)

    to apply the filter.

    Thanks anyway, your script was a great help!

Leave a Reply

Colin Phillips

itgroove Alumni

Colin Phillips

Subscribe via Email

Categories

Powered by...

itgroove Corporate Profile
Connect With Us