Some scenarios require a specific selection of folders and the root level files to be migrated opposed to a single path. This is fairly easy to accomplish using ShareGate and PowerShell.

So imagine a typical (central computing) homedrive environment for a user containing both personal documents and technical files required for the user profile to work.

This would include folder names like “Windows” or “Logs”. And apart from the usual “Documents” and “Pictures” folders, also some separate personal documents contained in the root folder.

In this scenario, the selection of document- and separate folders need to be migrated to OneDrive for Business for multiple users in bulk. This can be accomplished using ShareGate and PowerShell using the following guidance.
This post assumes that your are proficient in ShareGate and automation using PowerShell.

Exclude specific folders

In the beginning of your script, add an array-type variable including the names of the folders to exclude in the value.

#Comma separated list of folders to exclude
[array]$folderExclusions = @('Windows', 'Logs')

This list of values will be processed when iterating through the root folders in the homedrive, effectively excluding them in the selection.

Create and populate the array

Next, create another array that will be used to store the paths to include for the migration.

#Define the variable to store the paths
[array]$sourceItems = $null

The following cmdlets will populate the ‘sourceItems’ array with the folder paths directly under the homedrive path, excluding the ones mentioned in the ‘folderExclusion’ array.

#Get all eligible folders
[array]$sourceItems += Get-ChildItem -Path $row.sourceFolder -Exclude $folderExclusions | Where-Object { $_.PSIsContainer -eq $true } | ForEach-Object { ([System.Uri]$_.FullName) }

Next, the ‘sourceItems’ array will be further populated with the paths for all separate documents in the homedrive path.

#Get all eligible files
[array]$sourceItems += Get-ChildItem $row.sourceFolder | Where-Object { $_.PSIsContainer -eq $false } | ForEach-Object { ([System.Uri]$_.FullName) }

Start the migration

And now comes the cool part. the ShareGate ‘Import-document’ cmdlet has a ‘SourceFilePath’ parameter that can process the array and migrated the documents in the root folders and all folders (and underlying content) excluding those folders specified in the ‘folderExclusions’ array.

Import-Document  `
 -SourceFilePath $sourceItems `
 -DestinationFolder $importFolderName `
 -DestinationList $list `
 -CopySettings $copySettings `
 -Template $propertyTemplate `
 -MappingSettings $mappingSettings `
 -InsaneMode `
 -WaitForImportCompletion

Please note that the example above also requires preparation of the typical settings to successfully use this cmdlet and perform the migration. These settings are not included in this post.

The advantage of this approach is that all required paths can be migrated in a single ShareGate job instead of separate ones.