How To: Perform Bulk Uploads of Files to SharePoint

This post is going to discuss how to perform a bulk file copy from a folder on the file system to a SharePoint document library. 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 the file system to SharePoint, required metadata may need to be populated via default values or via modification to the script. Additional work could be performed in order to import the metadata from an xml file. This functionality is currently not available in the version of the script provided. This version of the script simply takes exactly what is on the file system and duplicates it in a SharePoint document library.

 

Background

 

I was recently working with a customer who needed to copy files from one SharePoint environment to another SharePoint environment. Although SharePoint does have this functionality built-in via Export-SPWeb and Import-SPWeb, this was not successful in the referenced environment. This was later determined to be related to corruption in the environment.

 

Approach

 

In this case, the approach was simple enough. Read the folder and file structure on the file system and replicate that structure in SharePoint.

 

Solution

 

The solution in this case is a little simpler than what was needed to originally export files from SharePoint to the file system, but the same basic principles apply. What's important in this case is to ensure that we create the folder structure before we start putting files in those folders. In order to do this, we need to be able to separate files from folders. We can use Get-ChildItem in order to accomplish this. Since we are only looking for folders, we can apply a filter.

Once we have a recursive list of all folders, we can loop through this list, creating each folder, and uploading the files in each folder as we go along. Retrieving a list of all files is also achieved by using Get-ChildItem using a slightly different set of filters.

Creating folders is done by using the $SPList.Folders.Add method. When we call this method, we'll have to pass the URL of the folder, the type of object to create, and the name of the folder. We also need to ensure that the folder is published, which can be performed by setting the "_ModerationStatus" property to "0". This is illustrated in the screenshot below.

Uploading Files to SharePoint is also relatively simple using PowerShell. We can use the $SPFolder.folder.files.add method for this. When we call this method, we need to pass a filename, the file stream, and a Boolean of whether or not to overwrite. We then need to perform certain tasks such as checking in, publishing and approving the file. This is illustrated in the screenshot below.

Once completed, the source directory and the destination library should look identical.

 

Download the Script

 

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

 

Usage

 

This script does require some edits. All edits are explained at the top of the script. If you are using multiple source directories and/or multiple destination directories these can be chained together in a single script by inserting multiple iterations of the sample script block. Each source folder and destination library will need to be added to the script using the three lines of PowerShell which are included. Here is a brief explanation.

This line indicates which subfolder within the directory should be used for source files. Do note that this will be appended to the $Directory parameter set higher up in the script:
$SourceFolder = "PowerShell Scripts"

This line indicates which library within the SharePoint site collection the files should be imported to. This parameter should be the title of an existing list in the SharePoint environment:
$DestinationLibrary = $Site.RootWeb.Lists | ? {$_.title -eq "Shared Documents"}

This line calls the ImportFiles function and uses the source folder and the destination library as parameters:
ImportFiles ($Directory + "\" + $SourceFolder) $DestinationLibrary

 

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