Get-DistributionListMembers.ps1


Description

Purpose

Retrieves the members of a distribution list.

Detailed Description

The Get-DistributionListMembers function retrieves the members of a distribution list based on the provided distribution list name. It uses the Get-DistributionGroup and Get-DistributionGroupMember cmdlets to fetch the distribution groups and their members.

Back to Top

Usage

Example 1

Get-DistributionListMembers -DistributionListName "Sales"

This example retrieves the members of the distribution list with the name “Sales”.

Back to Top

Notes

Author: Your Name Date: Today’s Date

Back to Top


Script

<#
.SYNOPSIS
Retrieves the members of a distribution list.

.DESCRIPTION
The Get-DistributionListMembers function retrieves the members of a distribution list based on the provided distribution list name. It uses the Get-DistributionGroup and Get-DistributionGroupMember cmdlets to fetch the distribution groups and their members.

.PARAMETER DistributionListName
The name of the distribution list for which to retrieve the members.

.EXAMPLE
Get-DistributionListMembers -DistributionListName "Sales"

This example retrieves the members of the distribution list with the name "Sales".

.INPUTS
System.String

.OUTPUTS
System.Management.Automation.PSCustomObject

.NOTES
Author: Your Name
Date: Today's Date
#>

function Get-DistributionListMembers {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$DistributionListName
    )

    process {
        if ($PSCmdlet.ShouldProcess($DistributionListName, 'Get-DistributionListMembers')) {
            try {
                $distributionGroups = Get-DistributionGroup | Where-Object { $_.Name -like "$DistributionListName" }

                if ($null -eq $distributionGroups) {
                    Write-Error "No distribution group found with the name: $DistributionListName"
                    return
                }

                $distributionGroups | ForEach-Object {
                    $groupName = $_.Name
                    $members = Get-DistributionGroupMember -Identity $groupName
                    $members | ForEach-Object {
                        $member = $_
                        $member | Select-Object DisplayName, PrimarySMTPAddress, SamAccountName, Alias, RecipientType, EmailAddresses, @{Name = 'DistributionGroupName'; Expression = { $groupName } }
                    }
                }
            }
            catch {
                Write-Error $_.Exception.Message
            }
        }
    }
}

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