How to Copy Office 365 Group membership to another user using PowerShell?

Requirement: Copy group membership to another user in Office 365.

PowerShell to Copy Office 365 Group Memberships

If you want to clone the memberships of all groups in an Office 365 environment, use this PowerShell script. Make sure the Azure AD module is installed prior to executing this script.

#Parameters
$SourceUserAccount = "[email protected]"
$TargetUserAccount = "[email protected]"

#Connect to Azure AD
Connect-AzureAD

#Get the Source and Target users
$SourceUser = Get-AzureADUser -Filter "UserPrincipalName eq '$SourceUserAccount'"
$TargetUser = Get-AzureADUser -Filter "UserPrincipalName eq '$TargetUserAccount'"

#Check if source and Target users are valid
If($SourceUser -ne $Null -and $TargetUser -ne $Null)
{
    #Get All memberships of the Source user
    $SourceMemberships = Get-AzureADUserMembership -ObjectId $SourceUser.ObjectId | Where-object { $_.ObjectType -eq "Group" }

    #Get-AzureADUserOwnedObject -ObjectId $SourceUser.ObjectId

    #Loop through Each Group
    ForEach($Membership in $SourceMemberships)
    {
        #Check if the user is not part of the group
        $GroupMembers = (Get-AzureADGroupMember -ObjectId $Membership.Objectid).UserPrincipalName
        If ($GroupMembers -notcontains $TargetUserAccount)
        {
            #Add Target user to the Source User's group
            Add-AzureADGroupMember -ObjectId $Membership.ObjectId -RefObjectId $TargetUser.ObjectId
            Write-host "Added user to Group:" $Membership.DisplayName
        }
    }
}
Else
{
    Write-host "Source or Target user is invalid!" -f Yellow
}

Please note, this script gets all group memberships including Microsoft 365 groups / Unified groups, Security Groups, Distribution Lists, and Mail enabled security groups of the given user where he’s added as a member of the group. To get all groups where the user is an owner, use:

$Ownerships  = Get-AzureADUserOwnedObject -ObjectId $SourceUser.ObjectId | Where-object { $_.ObjectType -eq "Group" 

Here is the PowerShell script to copy the user’s group ownership:

#Parameters - UPN
$SourceUserAccount = "[email protected]"
$TargetUserAccount = "[email protected]"

#Connect to Azure AD
Connect-AzureAD

#Get the Source and Target users
$SourceUser = Get-AzureADUser -Filter "UserPrincipalName eq '$SourceUserAccount'"
$TargetUser = Get-AzureADUser -Filter "UserPrincipalName eq '$TargetUserAccount'"

#Check if source and Target users are valid
If($SourceUser -ne $Null -and $TargetUser -ne $Null)
{
    #Get All Groups where the Source user is a Owner
    $SourceOwnerships = Get-AzureADUserOwnedObject -ObjectId $SourceUser.ObjectId | Where-object { $_.ObjectType -eq "Group" }

    #Loop through Each Group
    ForEach($Ownership in $SourceOwnerships)
    {
        #Check if the user is not part of the group
        $GroupOwners = (Get-AzureADGroupOwner -ObjectId $Ownership.Objectid).UserPrincipalName
        If ($GroupOwners -notcontains $TargetUserAccount)
        {
            #Add Target user to the Source User's group
            Add-AzureADGroupOwner -ObjectId $Ownership.ObjectId -RefObjectId $TargetUser.ObjectId
            Write-host "Added user to Group:" $Ownership.DisplayName
        }
    }
}
Else
{
    Write-host "Source or Target user is invalid!" -f Yellow
}

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!

Leave a Reply