Automating your file share migration to SharePoint with PowerShell

default post thumbnails

**A while back, we heard about a migration project Stephen had using Sharegate and some custom PowerShell scripting. We asked him if he’d kindly write an article detailing his script so that everyone could benefit from it. **

Migrating data is often, if not always, a complex task or project. Therefore each migration must be approached humbly. If you fail in one phase, you’ll most likely have to start the migration over again. The phase which is most complex and most time consuming is when you analyze data from a legacy system and map it to the destination system.

If the destination system is SharePoint, questions like these might occur: Which data types should be used for metadata? Should we use a list or a library? How do we navigate correctly to our data?

The Sharegate PowerShell commands are a powerful tool to execute repetitive migration tasks. Such tasks can occur when doing simple migrations several times. Automating parts of this process minimizes the amount of errors.

An example of a simple migration could be migrating documents from a legacy system to a SharePoint document library using the same set of metadata. This is what I use it for, which is why I have created this script below.

If you want to automate your Microsoft admin tasks, try these PowerShell script examples to help you boost productivity.

The SharePoint Migration Process

For the sake of this blog post, let’s assume we have all files from the legacy system as documents, Excel, Word, PDF files etc. When I migrate documents from file shares to SharePoint, my process goes as follows: Upload documents to a SharePoint test server, modify the documents using some custom script and copy the document library to the correct site as illustrated below. I use a custom non-PowerShell script to change created by and modified by on the documents.

SharePoint Migration stages

Uploading Documents To SharePoint

With this said, let us dive into the script. The first step is to import the Sharegate module, connect to your Site and Document Library as described here. Next, we need to have a property mappings file for our migration. To do so, setup your profile and export it to the file system.

Sharegate Migration Mappings

Next we need to specify a path to where Sharegate should save our migration report file.

The final set of variables looks like this:

Setting the Mappings

Once we have specified this, we can set the mappings. For this I have created a function called SetMappings. I will set the property mappings to the file we specified in the previous step and set the group “Unresolved user or group” to System Account. If we don’t do this, the migration will give an error on items where a User or Group could not be found. The script for this function looks like this:

Importing Documents To SharePoint

Now that we’ve setup the variables for our destination Site and setup the property mappings, we want to upload the documents and save the migration report afterwards. To do so, we use the Import-Document command and Export-Report command.

The result is the following:

Copying The SharePoint Document Library

Once we’ve imported the documents to the Document Library on the test server, we will run any custom scripts on the set of documents manually. This could be to change some metadata or change the content of the documents. Afterwards we can copy the Document Library using the function Copy-List and export the result.

Now put it all together. The script run in the command window looks like this:

SharePoint migration PowerShell script

The script can be modified as you wish by drawing from the full power of PowerShell. One improvement could be to delete and create the document library each time the script is run. This could be run from the server, but in most cases you want to create client side scripts. Another improvement could be to add exception handling when the Sharegate PowerShell commands are used.

I’m not a PowerShell guy, and I haven’t really used PowerShell before, but the scripting part was pretty straight forward. I used the command index a lot and it proved very useful.

To sum it all up, for repetitive tasks in a simple SharePoint migration, such as moving documents from file shares to a Document Library using the same set of metadata, use Sharegate PowerShell commands to minimize the amount of errors. Define the migration process and automate it.


For more articles on file share migration you can read the following blogposts


What did you think of this article?

Recommended by our team

Getting started is easy

Try ShareGate free for 15 days. No credit card required.

Hosts 1

LIVE VIRTUAL EVENT RISE UP: Ignite your M365 tenant migration playbook