Compartilhar via


How to: Perform Bulk Downloads of Files in SharePoint

 

This post is going to discuss how to perform a bulk file copy from a SharePoint library to a folder on the file system. Along with this blog post, I will also provide a downloadable script, which can be modified to work in your environment. As we are copying files from SharePoint to the file system, all metadata will be stripped. Additional work could be performed in order to export the metadata to an xml file. This functionality is currently not available in the version of the script provided.

 

Background

 

I was recently working with a customer who needed to export files from one SharePoint environment and import them into another SharePoint environment. Though SharePoint does have this functionality built-in using Export-SPWeb and Import-SPWeb, in this case an export resulted in no files being exported from the desired directory. This was later determined to be corruption of the files after the export was unsuccessful on the desired directories.

 

Approach

 

In this case, the approach was simple enough. Read the folder and file structure from the specified list and replicate that structure on the file system.

 

Solution

 

This solution is tricky enough because of the way files are accessed in SharePoint lists. Enumerating all files in the list can be performed, however folder structure would not be maintained by calling $List.Items. In this case, what we have to do is perform two separate actions. One action to retrieve the root folder of the list and enumerate the items, and one action to enumerate each folder and each file in each folder. In order to do this, we call $List.rootfolder.files

We also need to enumerate all folder, and enumerate all of the files in each folder. This can be done by using $List.folders. This will list all folders (including subfolders) for the list.

$Folder.folder.files allows you to enumerate all files in a specific folder

 

Now that we have a collection of all of the files and their directories, we can export the files to the file system. This can be performed by accessing the binary steam of the SharePoint file, and assigning it to the binary stream of a file system object. This is performed by using the following snippet of code.

 

Once the script has been exported, the structure of the files and folders will be maintained on the file system as they were in the SharePoint site.

 

Download the Script

 

The script can be downloaded from the following location:
BulkFileExport.ps1 (compressed)

 

Usage

 

This script does require some edits. All edits are explained at the top of the script. Each child directory must be specified in the script. Each library which is to be exported will need to be added to the script using the three lines of PowerShell which are included. Here is a brief explanation.

This next line will create a directory at the specified location. In this example, a directory will be made beneath the root directory and will be named StyleLibrary
New-Item -Path ($Directory + "\StyleLibrary") -ItemType Directory

 

This next line will retrieve a list with the specified title, and assign it to a variable. The variable name is not important, as long as the correct variable is passed to the ExportFiles Function in the next line
$StyleLibrary = $Site.RootWeb.Lists | ? {$_.title -eq "Style Library"}

 

This next line will export files from the list specified via the first variable and to the subdirectory specified in the second variable.
ExportFiles $StyleLibrary "StyleLibrary"

 

Feedback

 

As always, feedback and suggestions are always welcome. If you do have any ideas on how to improve the script, I'd love to hear them.

You can also follow me on Twitter:

 RCormier_MSFT

Comments

  • Anonymous
    February 17, 2014
    The comment has been removed

  • Anonymous
    March 24, 2014
    The comment has been removed

  • Anonymous
    December 24, 2014
    Great article, thanks for your information, I used this SharePoint auditing tool( www.lepide.com/sharepoint-audit) that allows to export all sites, list, content,group, files and folder and track all events in some particular site collections including sensitive data and to collect data from multiple SharePoint Servers and view data of who is doing what and where in your SharePoint environment.

  • Anonymous
    October 28, 2016
    The comment has been removed