Copy-DistributionGroupMembership.ps1


Description

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.

Back to Top

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.

Back to Top

Notes

Author: Unknown Date: Unknown

Back to Top


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
            }
        }
    }
}

Back to Top

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.

Issue


Back to Top