Copy-DistributionGroupMembership.ps1
19 Sep 2025Description
Purpose
Copies the distribution group membership of one user to another user.
Detailed Description
This function copies the distribution group membership of one user to another user. It can also align the destination user’s distribution group membership with the source user’s.
Usage
Example 1
Copy-DistributionGroupMembership -SourceUser "User1" -DestinationUser "User2" -AlignMembership
Copies the distribution group membership of User1 to User2 and aligns User2’s distribution group membership with User1’s.
Notes
Author: Unknown Date: Unknown
Script
Function Copy-DistributionGroupMembership {
<#
.SYNOPSIS
Copies the distribution group membership of one user to another user.
.DESCRIPTION
This function copies the distribution group membership of one user to another user. It can also align the destination user's distribution group membership with the source user's.
.PARAMETER SourceUser
The SamAccountName of the user you are copying from.
.PARAMETER DestinationUser
The SamAccountName of the user you are copying to.
.PARAMETER AlignMembership
If specified, aligns the destination user's distribution group membership with the source user's.
.EXAMPLE
Copy-DistributionGroupMembership -SourceUser "User1" -DestinationUser "User2" -AlignMembership
Copies the distribution group membership of User1 to User2 and aligns User2's distribution group membership with User1's.
.NOTES
Author: Unknown
Date: Unknown
#>
[CmdletBinding(
SupportsShouldProcess = $true
)]
param (
[Parameter( Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Enter the SamAccountName for the user you are copying from."
)]
[string]
$SourceUser,
[Parameter( Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = "Enter the SamAccountName of the user you are copying to."
)]
[string]
$DestinationUser,
[Parameter(
HelpMessage = "Align the destination user's distribution group membership with the source user's."
)]
[switch]
$AlignMembership
)
process {
if ($PSCmdlet.ShouldProcess("$DestinationUser", "Copy User $SourceUser Distribution Group Memberships")) {
$SourceUserGroups = Get-DistributionGroup -ResultSize Unlimited | Where-Object { (Get-DistributionGroupMember -Identity $_.Identity -ResultSize Unlimited).PrimarySmtpAddress -contains $SourceUser }
if ($AlignMembership) {
$DestinationUserGroups = Get-DistributionGroup -ResultSize Unlimited | Where-Object { (Get-DistributionGroupMember -Identity $_.Identity -ResultSize Unlimited).PrimarySmtpAddress -contains $DestinationUser }
foreach ($Group in $DestinationUserGroups) {
if ($SourceUserGroups -notcontains $Group) {
Remove-DistributionGroupMember -Identity $Group.Identity -Member $DestinationUser -Confirm:$false
}
}
}
foreach ($Group in $SourceUserGroups) {
Add-DistributionGroupMember -Identity $Group.Identity -Member $DestinationUser -ErrorAction SilentlyContinue
}
}
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.