SharePoint Online: Clone User Group Memberships using PowerShell

Requirement: Clone a user’s group permissions to another user in a SharePoint Online Site

PowerShell to Clone User Group Memberships in SharePoint Online

PowerShell to Clone User’s Group Memberships in SharePoint Online:

It’s a common real-world scenario that when someone leaves the organization, and somebody else takes that role, they want the same permissions! While the requirement sounds simple, it’s cumbersome to achieve it through the web user interface. Basically, you have to go to each group where the source user is a member and add the target user to it. We can automate this task using PowerShell to clone user group memberships from one account to another!

#Function to copy group memberships from one user to another
Function Clone-SPOUserGroupPermission($SiteURL, $SourceUserID, $TargetUserID)
{
    Try {
        #Get the Site collection
        $Site = Get-SPOSite $SiteURL 
    
        #Get the Source user
        $SourceUser = Get-SPOUser -Site $Site -LoginName $SourceUserID

        #Get All groups of the source User
        $UserGroups = $SourceUser.Groups
    
        If($UserGroups.Count -gt 0)
        {
            Write-host -f Yellow "Found the User Member of $($UserGroups.Count) Group(s)..."

            #Loop through each group of the source user and add target user to that group
            ForEach ($Group in $UserGroups)
            {
                #Add Target User to the Group
                Add-SPOUser -Site $Site -LoginName $TargetUserID -Group $Group | out-null    
                Write-Host -f Green "Added $TargetUserID to Group '$Group'"
            }
        }
    }
    Catch {
        Write-host $_.Exception.Message -f Red
     }
}

#SharePoint Online Admin Center URL
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$SourceUserID ="[email protected]"
$TargetUserID ="[email protected]"

#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

#Call the Function
Clone-SPOUserGroupPermission -SiteURL $SiteURL -SourceUserID $SourceUserID -TargetUserID $TargetUserID 

You can also use the “Get-SPOSite” cmdlet to get all site collections and call the function to copy the user’s group memberships to another user.

What if you want to copy permissions completely from one user to another instead of just copying group permissions? Because the source user may have permissions in other levels too. E.g., Permission may be granted directly (such as “Full Control”), as a site collection admin, at list/folder/item levels! So, Here is the PowerShell to clone user’s permissions: How to Copy User Permissions in SharePoint Online using PowerShell?

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “SharePoint Online: Clone User Group Memberships using PowerShell

  • Hi Sir,

    Is there any way to clone user on entire tenant level?

    Thanks,
    V

    Reply

Leave a Reply