top of page

Using Sharegate to Bulk Migrate Sharepoint Folders

  • Charles Smith
  • Oct 6, 2020
  • 2 min read

If you need to migrate Sharepoint folders in bulk, but not simply migrate the whole site, (i.e. you need to selectively migrate certain Sharepoint folders and not others), with Sharegate you will need to use Sharegate Powershell as the GUI does not handle this very easily.



There is one caveat in this process though, whilst the Sharegate GUI will create the folders automatically in the destination, Sharegate Powershell will not, the top level folder you are migrating will need to be created first. Subfolders are created automatically though.


Below are two scripts, the first to create the folders, and the second to copy (migrate) the folders.


Both will read in a CSV in the format ( SourceLibrary, SourceFolder) where 'SourceLibrary is the name of the document library and 'SourceFolder' the name of the folder to copy.


Note these can be used to migrate between Sharepoint Online site, on prem to on prem, and from on prem to Sharepoint Online.


Folder creation:


##### Set your site name here, the full URL #####

$SiteUrl = "https://yourdomain.sharepoint.com/sites/sitename"


##### Set your admin username here #####

$username = 'admin@yourdomain.com'

$password = Read-Host 'Enter Password' -AsSecureString


##### Set your input file #####

$folderlist = 'c:\temp\folderlist.csv'

#Setup Credentials to connect

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$password)


#Set up the context

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)

$Context.Credentials = $credentials

import-csv $folderlist | foreach-object -process {


$library = $_.SourceLibrary


##### Set your site name here #####

$ListURL="/sites/sitename/$Library"


#Get the List Root Folder

$ParentFolder=$Context.web.GetFolderByServerRelativeUrl($ListURL)


$foldername = $_.sourcefolder

#sharepoint online powershell create folder

$Folder = $ParentFolder.Folders.Add($FolderName)

$ParentFolder.Context.ExecuteQuery()

}



Folder Migration:



$StartDate = Get-Date


##### set your admin accounts for the source and destination environments #####

$srcuser = 'admin@source.com'

$destuser = 'admin@destination.com


##### set your source and destination sites #####

$SourceSiteURL = 'https://mysharepointsite.com/sites/sitename'

$DesitnationSiteURL = 'https://domain.sharepoint.com/sites/sitename'


##### Set your input file #####

$folderlist = 'c:\temp\folderlist.csv



$srcPassword = Read-Host -Prompt "Enter password for source user" -AsSecureString

$destPassword = Read-Host -Prompt "Enter password for destination user" -AsSecureString



$srcSite = Connect-Site -Url $SourceSiteURL -Username $srcuser -Password $srcPassword

$dstSite = Connect-Site -Url $DesitnationSiteURL -Username $dstuser -password $destPassword


$count = 0

$copysettings = New-CopySettings -OnContentItemExists IncrementalUpdate #IncrementalUpdate ensures that when you run the same CSV again, it'll avoid duplicates


Write-Host "Copy Started: " $StartDate

import-csv $folderlist | foreach-object -process {


#read variables from each line in CSV:

$sourceLibName = $_.SourceLibrary #source library name

$destLibName = $_.DestinationLibrary #destination library name

$sourcefolder = $_.sourcefolder #source folder name for 'Account' folders

write-host $destlibname

$count++

Write-Host "Count: " $count " copying from: " $srcsite$sourceLibName$sourcefolder " to: " $dstsite$destLibName


$dstLb = Get-List -Name $destLibName -Site $dstSite

$srcList1 = Get-List -Name $sourceLibName -Site $srcSite

Copy-Content -SourceList $srcList1 -SourceFolder $sourcefolder -DestinationList $dstLb -CopySettings $copysettings -destinationfolder $sourcefolder


}


$EndDate = Get-Date

Write-Host "Copy Ended: " $EndDate

$Diff = $EndDate - $StartDate

Write-Host "Difference: " $Diff


Comments


©2020 by CVSConsult Ltd

bottom of page