Generate User Mapping For Sharegate Migration Using PnP PowerShell

Use the below PowerShell code to generate the User Mapping file (.sgum) for Sharegate Desktop migration. Click on the "Import user and group mapping" button as mentioned in the screenshot below. Once the mapping is updated then you can see Source and Destination Users and Groups will be mapped under the "Source" and "Destination" columns. The Sharegate tool will map your users automatically and migrated them.

You have another option "Unresolved users or groups", this will use to map user on target which is missing. You can replace the User on target which is existed at the source but does not exist at the destination.

Use the below code in PowerShell.

Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add - Type - Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#This code is use for get current folder path from where you are running.ps1 file
#Generated User Mapping will be saved on same path with extension.sgum[string] 

$scriptName = $MyInvocation.MyCommand.Name.TrimEnd(".ps1");
[string] $scriptFullPath = $MyInvocation.MyCommand.Definition;
[string] $scriptFileFolder = Split - Path - Path $scriptFullPath - Parent;
$currentDate = Get - Date - format ddMMyyyy - hhmmss;
$errorFile = "UserMapping_$($currentDate).csv"
$errorDetails = "SourceURL,Status"
$errorDetails | Out - file $errorFile - append

function GenerateUserMapping() {
    #Source URL
    $sourceURL = "USE SELECT SOURCE SITE URL TO GET ALL USERS";
    #Connect to Source URL using PNP connection
    Connect - PnPOnline $sourceURL - UseWebLogin
    $sourceClientContext = Get - PnPContext
    $sourceWeb = $null;
    try 
    {
        #Get Source URL Wen context
        $sourceWeb = $sourceClientContext.Web;
        $sourceClientContext.Load($sourceWeb);
        $sourceClientContext.ExecuteQuery();
    } 
    catch 
    {
        [string] $FuncName = $MyInvocation.MyCommand.Name;
        Write - Host - Message $_ - FunctionName $FuncName;
        $errorDetails = "$sourceURL, error"
        $errorDetails | Out - file $errorFile - append
    }

    #Get all Users
    $allUsers = $sourceWeb.SiteUsers
    $sourceClientContext.Load($allUsers)
    $sourceClientContext.ExecuteQuery()
    #This function is used for to create actual file

    generateUserMappingFile $Users $sourceURL
}

function generateUserMappingFile ($allUsers, $sourceURL) 
{
    $NameofFile = "PROVIDE ANY NAME FOR FILE"
    $XMLFile = "$scriptFileFolder\UserMapping_$NameofFile.sgum"
    #Create new XML File[System.XML.XMLDocument] 
    $XMLDocument = New - Object System.XML.XMLDocument
    #Add XML Declaration
    $Declaration = $XMLDocument.CreateXmlDeclaration("1.0", "UTF-8", $null)
    $XMLDocument.AppendChild($Declaration)
    #Create Root Node[System.XML.XMLElement] 
    $XmlMainRoot = $XMLDocument.CreateElement("UserMappings")[System.XML.XMLElement] 
    $XMLRoot = $XMLDocument.CreateElement("Mappings")
    $allUsers | ForEach - Object 
    {
        #Add child node "Item"
        if ($_.Email - ne "") 
        {
            $ItemElement = $XMLDocument.CreateElement("Mapping")
            $ItemElement1 = $XMLDocument.CreateElement("Source")
            $ItemElement1.SetAttribute("AccountName", $_.LoginName)
            $ItemElement1.SetAttribute("DisplayName", $_.LoginName)
            $ItemElement1.SetAttribute("PrincipalType", "None")
            $ItemElement2 = $XMLDocument.CreateElement("Destination")
            $ItemElement2.SetAttribute("AccountName", "i:0#.f|membership|$($_.Email)")
            $ItemElement2.SetAttribute("DisplayName", "i:0#.f|membership|$($_.Email)")
            $ItemElement2.SetAttribute("PrincipalType", "None")
            $ItemElement.AppendChild($ItemElement1)
            $ItemElement.AppendChild($ItemElement2)
            $XMLRoot.AppendChild($ItemElement)
        }
    }

    # Append Root Node to XML
    $XmlMainRoot.AppendChild($XMLRoot)
    $XMLDocument.AppendChild($XmlMainRoot)
    #Save XML File
    $XMLDocument.Save($XMLFile)
}

try 
{
    GenerateUserMapping
} catch 
{
    [string] $FuncName = $MyInvocation.MyCommand.Name;
    Write - Host - Message $_ - FunctionName $FuncName;
}