Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Exclude folders from ShareGate fileshare migration

If you would like to do a fileshare migration to OneDrive, SharePoint Online or MS Teams, sometimes you would like to skip some specific technical folders like browser profile folders or something. With this script you can loop into all folders in the fileshare and exclude some specific folders.

Import-Module Sharegate # Define the CSV file path 
$csvFile = "F:\scripts\Migration\input2.csv" # Import the CSV file 
$table = Import-Csv $csvFile -Delimiter ";" # Define folders to exclude 
$excludeFolders = @("Edge_Profile", "Chrome_Profile", "FireFox_Profile", "AppDate", "Google", "MsOffice", "Personal_Settings", "Outlook")
$copysettings = New-CopySettings -OnContentItemExists Skip
$mappingSettings = New-MappingSettings
$mappingSettings = Set-UserAndGroupMapping -MappingSettings $mappingSettings -UnresolvedUserOrGroup -Destination "_svc_migratie1"
$spAdminCenterUrl = "https://myCompany-admin.sharepoint.com"
$libraryName = "Documenten" # if the English language is used for OneDrive, you need to use the library name 'Documents' 

Write-Host "Connect to SharePoint admin center $spAdminCenterUrl"
$connection = Connect-Tenant -Domain $spAdminCenterUrl -Browser

# Loop through each row in the CSV 
foreach ($row in $table) { 
    Write-Host "Prepare migration for source '$($row.DIRECTORY)' to OneDrive target '$($row.ONEDRIVEURL)'" -ForegroundColor Blue
    
    # Connect to the OneDrive site 
    Write-Host "Connect to OneDrive $($row.ONEDRIVEURL)"
    $dstSite = Connect-Site -Url $row.ONEDRIVEURL -UseCredentialsFrom $connection

    # Get the "Documents" library from the destination site
    Write-Host "Get library '$libraryName'" 
    $dstLibrary = Get-List -Name $libraryName -Site $dstSite

    if($dstLibrary){
        # Import from file share to OneDrive, excluding specified folders 
        Write-Host "Migrate source '$($row.DIRECTORY)' to OneDrive target '$($row.ONEDRIVEURL)'"
        
        #$Source = $row.DIRECTORY.replace('\\ams.intra.myCompany.nl\data','Y:')
        $source = $row.DIRECTORY
        $folders = Get-ChildItem -Path $source -Exclude $excludeFolders

        # Migrate folders
        foreach ($folder in $folders)
        {
            try
            {
                Write-Host "Migrating '$folder'" -foregroundcolor Yellow 
                $uriFolder = [System.Uri]$folder.FullName
                $result = Import-Document -SourceFilePath $uriFolder -DestinationList $dstLibrary -CopySettings $copysettings -MappingSettings $mappingSettings -ErrorAction Stop 
            }
            catch
            {
                Write-Host "Error migrating '$folder' from fileShare '$($row.DIRECTORY)' to OneDrive '$($row.ONEDRIVEURL)'" -ForegroundColor Red
            }
        }

        Write-Host "Succesfully migrated source '$($row.DIRECTORY)' to OneDrive target '$($row.ONEDRIVEURL)'"
    } else {
        Write-Host "Cannot find target library by name '$libraryName' in OneDrive target '$($row.ONEDRIVEURL)'"
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *