This PowerShell script has two use cases combined in it – ADD and REPLACE. Before any change is made the script also shows a group membership for both a target and a source users.
Note, the script only processes those distribution groups that are managed in the cloud. If you have some groups synced with Active Directory, those should be processed separately.
USE CASE #1 – ADD
When Add is chosen, the scripts only adds distribution groups of the source user to the target user’s list.
Use this part of the script when a target user assumes new responsibilities that another user has but keeps the old responsibilities as well. In other words, the target user needs to have a combination of new and old distribution groups to perform job tasks.
USE CASE #2 – REPLACE
Essentially, the 2nd part of the script is a combination of the 1st part and another Script to remove a user from all distribution groups managed in the cloud
The scrips clears the target user’s group membership and then replaces with the distribution groups the source user has.
A good example for it – a user moving to a different job position, similar to the one another person has. That different position implies
$sourceemail = Read-Host "Please provide an email address of the 'source user' (distribution groups of this person will be used to add or replace the ones of the target user)" $mailbox1 = Get-Mailbox -Identity $sourceemail $DN1=$mailbox1.DistinguishedName $Filter1 = "Members -like ""$DN1""" $DistributionGroupsList1 = Get-DistributionGroup -ResultSize Unlimited -Filter $Filter1 Write-host `n Write-host "Listing all Distribution Groups of the source user" Write-host `n $DistributionGroupsList1 | ft $targetemail = Read-Host "Please provide an email address of the 'target user' (distribution groups of this person will be replaced or added to using the 'source user')" $mailbox2 = Get-Mailbox -Identity $targetemail $DN2=$mailbox2.DistinguishedName $Filter2 = "Members -like ""$DN2""" $DistributionGroupsList2 = Get-DistributionGroup -ResultSize Unlimited -Filter $Filter2 Write-host `n Write-host "Listing all Distribution Groups of the target user" Write-host `n $DistributionGroupsList2 | ft $answer = Read-Host "What procedure would you like to do (add / replace / n [to cancel])?" While ("add","replace","n" -notcontains $answer) { $answer = Read-Host "What procedure would you like to do (add / replace / n [to cancel])?" } If ($answer -eq 'add') { ForEach ($item in $DistributionGroupsList1) { Add-DistributionGroupMember -Identity $item.DisplayName –Member $targetemail –BypassSecurityGroupManagerCheck } Write-host `n Write-host "Successfully added" Remove-Variable * -ErrorAction SilentlyContinue } ElseIf ($answer -eq 'replace') { ForEach ($item in $DistributionGroupsList2) { Remove-DistributionGroupMember -Identity $item.DisplayName –Member $targetemail –BypassSecurityGroupManagerCheck -Confirm:$false } ForEach ($item in $DistributionGroupsList1) { Add-DistributionGroupMember -Identity $item.DisplayName –Member $targetemail –BypassSecurityGroupManagerCheck } Write-host `n Write-host "Successfully replaced" Remove-Variable * -ErrorAction SilentlyContinue } ElseIf ($answer -eq 'n') { Remove-Variable * -ErrorAction SilentlyContinue }