PowerShell Script for OneDrive for Business Migration

PowerShell Script for OneDrive for Business Migration

by

It’s the 21st century, more and more businesses are moving their data from local on-premises file shares to the cloud for a variety of reasons. It could be for persistence and backups, ease of access for employees across the globe, or even to limit the amount of infrastructure required to run the business itself. Cloud storage encourages working from home and goes hand-in-hand with more modern work environments.

Here on the Sharegate support team, a common scenario our users ask us about is the need to move a traditional network file share to OneDrive for Business. The file share is usually name-based and mapped on each computer as a network drive. For example, the employee named Jimmy De Santis will have a mapped drive containing both public and personal documents set up as follows: DOMAINJimmy De Santis.

OneDrive for Business Migration Pre-Requisites

The person usually tasked with the migration (in this case, you!) generally has full control on the DOMAIN folder, so we’ll go upon the assumption that it’s mapped as a network drive for the rest of the walkthrough.

It’s also crucial that all the OneDrives have been previously provisioned. The hard way of doing this is making sure all your users have logged into Office 365. This will cue SharePoint to begin provisioning the user’s OneDrive. However, provisioning can be automated using Sharegate’s Get-OneDriveURL Powershell cmdlet.

You’ll also want to make sure the account being used to perform a OneDrive for Business migration is set as an administrator on each individual OneDrive. Luckily Sharegate’s Reporting tool will allow you to do this in a few clicks.

In short, use the software’s Reporting tool to pull up a list of all your OneDrives, select all the results that come up, click on Edit selected item(s) and run the “Add administrator” operation on them for the account you’ll be using for the migration.

Creating a custom report in Sharegate

Setting administrators OneDrive for Business in Sharegate report

Adding administrators to OneDrive for Business

Also of note: if the user account used to connect to your SharePoint tenant has the necessary rights to grant this permission level, the software will handle this portion itself!

Preparing Your Script

What we’re looking to do in this scenario is migrate the contents of DOMAINJimmy De Santis to the user’s OneDrive for Business, which, for demonstration purposes, is located at https://contoso-my.sharepoint.com/personal/jimmy_de_santis_contoso_com/ as well as repeat the operation for each user that’s a part of the company, all while preserving the permissions on each of the folders.

The first step in accomplishing this is to generate a list of the names of all the folders contained in the DOMAIN folder, along with another list of all their corresponding OneDrive URLs. This list will then be fed to the PowerShell script performing the migration, so that it can cycle through each folder and migrate it to the corresponding OneDrive for Business space.

There’s a simple PowerShell cmdlet that will allow you to list all the directories in a specified location. It goes as follows:

This will create a CSV file containing a list of all the directories contained in the DOMAIN mapped drive. Once this is done, you’ll also want to make sure you create a header column by inserting a line at the top of the document that’s generated named DIRECTORY as well as insert the correct mapped drive letter before the name of the directory contained in the column.

If listing all the OneDrive URLs using this method is too time-consuming, you can use Sharegate to quickly build a list of OneDrive URLs by going into the Reporting tool, running a search for OneDrive for Business and exporting the results to an Excel spreadsheet. Here’s a screenshot of the search you’ll want to run:

Performing a search with custom report in Sharegate

Once again, make sure you insert a header into the Excel file exported from the tool as well. In this case, I’d recommend naming it ONEDRIVEURL.

Once you have both lists, you can combine them either manually by doing a copy/paste job or using PowerShell. What you want to end up with is a CSV file with two columns, titled DIRECTORY and ONEDRIVEURL, with each directory correctly lined up with its corresponding OneDrive URL. Here’s a screenshot of what the CSV file should look like when opened in Excel:

Create csv file for OneDrive for Business Migration

If you’re a more advanced user, you can also leverage PowerShell to build the complete CSV file for you once you have both the list of directories and the OneDrive URLs.

The important part of building this CSV is making sure each directory listed in the DIRECTORY column matches up with the correct OneDrive URL in the cell next to it, under the column named ONEDRIVEURL, as can be seen in the screenshot above.

The Migration to OneDrive for Business

The script you’ll be running for your migration will then cycle through each row in your CSV file and use the Sharegate PowerShell module’s cmdlets to copy each folder to the correct user’s OneDrive default document library.

PowerShell has quite a few fun functions that will allow you to do so. You can automatically generate a table based off of a CSV file as follows:

This, along with a ‘foreach’ loop that cycles through the table created by this cmdlet are crucial to building your migration script as they ensure that all you need to do is feed the script your list of directories and their corresponding site to move all of your user’s documents into SharePoint.

Since you’re using Sharegate’s Import-Document cmdlet during the copy process, all of Sharegate’s benefits will come with the migration. A few (not-so) small examples are that file and folder permissions will be preserved, you’ll be able to specify user mappings and property templates during the process and you can also leverage your Azure storage to migrate your documents at speeds faster than ever before by setting up Sharegate’s Insane Mode first.

The joy of being able to use PowerShell to write a script for your migration is the sheer amount of flexibility that’s offered. More advanced users can customize pretty much everything down to the smallest details but for simplicity’s sake, the script below covers the basics only.

This is a basic migration script I wrote up that will allow you to use the CSV file mentioned above to perform your migration. Feel free to change it up a little for your own needs – there truly are a ton of possibilities available using the method to move your user’s files to OneDrive for Business.

Scheduling the Migration

Once you have your full script written up, you can schedule it to be run during off-hours to optimize performance or at any other time you prefer using Windows Task scheduler.

To run a PowerShell script at a scheduled time, begin by saving your completed (and obviously tested) script as a .ps1 file. Once this is done, tell Windows Task Scheduler to invoke PowerShell and pass the location of your script as a launch argument. Here’s a great TechNet article explaining exactly how to do so.

In the end, your hard work will truly pay off as all you’ll need to do is wait until the task runs – no painful manual work needed. You even have the possibility of tweaking your script a little in the future to migrate any other folders you need to move to a specific OneDrive, allowing you to save on both time and potential frustration.

Shane Poitras
Shane Poitras @SharegateTools

This Customer Happiness Guardian has great technical knowledge! When he isn't helping you better use our tool, he writes carefully crafted articles to help you leverage the full potential of Sharegate, so your life gets Just Damn Simple.