Quickly migrate your file shares to Teams via a PowerShell script (with role mappings!)

With all the migrations for customers I’ve done lately I was fed up with manually mapping all the file shares to Teams and setting all the roles from the Teams Admin center.
To overcome this hassle I’ve created a script which automatically migrates all your file shares to Teams to make the migration run fast and smooth.
There is even an option to create a mappings file which sets the permissions for the Teams.
Prerequisites
What do we need in order to use this script?
- The Microsoft Teams PowerShell module
- The Sharepoint Migration Tool
- The Sharepoint Online PowerShell Module
Some things to keep in mind
- This script will give the Global Admin you specify the Owner role on all of the Teams.
You can remove this access and/or change the owner of the Teams afterwards. - When the Global Admin uses MFA (which is a security default in Microsoft 365) you will need to log on to all the services separately. The script has a function set for this.
- The mappings file must be a CSV file and delimited by semicolons.
It must contain the Team’s name, the user in UPN format and the role (owner or member).
A sample file is included at the end of this blog post in the Downloads section.
What this script will do for you

- Create a variable containing all your folders names
- Connect to Microsoft Teams and Sharepoint Online PowerShell
- Create Teams from the variable you just created
- Set the correct roles on the Teams (if you specified a mapping file)
- Get the SharePoint URL’s from the Teams to be able to create mappings in the SharePoint Migration Tool Powershell Module
- Start a new migration from the tool
- Migrate the folders
The script
function AskMFA {
$mfa=read-host "Are you using an account which has MFA configured for this migration? (y/n)"
if ($mfa -eq "y") {Return MFA}
elseif ($mfa -eq "n") {Return NoMFA} else {write-host "Incorrect input, please enter y (Yes) or n (No)"; Return AskMFA}
}
function AskMapping {
$mapping=read-host "Do you want to use a mappings file? (y/n)"
if ($mapping -eq "y") {$mappingfile =read-host "Please enter the mappings file location e.g. C:\temp\example\mappings.csv";$mappings=import-csv -path $mappingfile -Delimiter ";"}
elseif ($mapping -eq "n") {} else {write-host "Incorrect input, please enter y (Yes) or n (No)"; Return AskMapping}
}
$source=read-host "Please enter the source you want to migrate e.g. C:\temp\example"
AskMapping
AskMFA
function NoMFA {
$Global:SPOUrl =read-host "Please enter your Sharepoint admin URL e.g. https://yourdomain-admin.sharepoint.com";
$Global:UserName = read-host "Please enter your Global Admin username e.g. admin@yourdomain.onmicrosoft.com"
$Global:PassWordPlain=read-host "Please enter your Global Admin password e.g. Th1sISmyP@ssw0rd;)"
$Global:PassWord = ConvertTo-SecureString -String $Global:PassWordPlain -AsPlainText -Force
$Global:SPOCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Global:UserName, $Global:PassWord
Register-SPMTMigration -SPOCredential $Global:SPOCredential -Force -KeepAllVersions $true -IncludeHiddenFiles $true -MigrateWithoutRootFolder
connect-sposervice -Url $Global:SPOUrl -Credential $Global:SPOCredential
Connect-MicrosoftTeams -Credential $Global:SPOCredential
Return StartMig
}
function MFA {
$Global:SPOUrl =read-host "Please enter your Sharepoint admin URL e.g. https://yourdomain-admin.sharepoint.com";
Write-host -foregroundcolor Yellow "Connecting to Sharepoint Online (1/3)"
connect-sposervice -Url $Global:SPOUrl
Write-host -foregroundcolor Yellow "Connecting to Microsoft Teams (2/3)"
Connect-MicrosoftTeams
Write-host -foregroundcolor Yellow "Registering Sharepoint Migration Tool migration (3/3)"
Register-SPMTMigration -Force -KeepAllVersions $true -IncludeHiddenFiles $true -MigrateWithoutRootFolder
Return StartMig
}
function StartMig {
$folders=Get-item -Path "$source\*"
cd "$env:UserProfile\Documents\WindowsPowerShell\Modules\Microsoft.SharePoint.MigrationTool.PowerShell"
Import-Module Microsoft.SharePoint.MigrationTool.PowerShell
foreach ($folder in $folders) {New-Team -DisplayName $folder.Name}
$teams=foreach ($folder in $folders) {get-team -DisplayName $folder.Name | select DisplayName,MailNickname,GroupId}
$SPsites=foreach ($team in $teams) {get-sposite | where {$_.Url -like "*/sites/$($team.MailNickName)"} | select Title,Url}
if ($mappings -eq $null) {} else {foreach ($user in $mappings) {Add-TeamUser -GroupId (get-team -DisplayName $user.Team).GroupId -Role $user.Role -User $user.User;get-team | select DisplayName,Members}}
foreach ($task in $SPsites) {Add-SPMTTask -FileShareSource "$source\$($task.Title)" -TargetSiteUrl $task.Url -TargetList "Documents" -TargetListRelativePath "General"}
Start-SPMTMigration
}
I hope this script will give you an easier and faster way to migrate file shares.
If you have any questions please let me know in the comments section below!