Script Repository


Dynamic distribution list members

June 23, 2020
958

The script creates a CSV-formatted report on members of a dynamic distribution list.

To generate such a report on request, you can create a Custom Command that runs the script on Ms-Exch-Dynamic-Distribution-List objects. For this purpose, on Step 2 of the Create Custom Command wizard, select Show all object types and Ms-Exch-Dynamic-Distribution-List.

Parameters:

  • $exchangeServer - specifies the fully qualified domain name (FQDN) of your Exchange Server;
  • $csvFilePath - specifies a full path to the CSV file containing the report;
  • $propertiesToExport - specifies properties of the members to include in the report.
Edit Remove
PowerShell
$exchangeServer = "exchangeserver.domain.com" # TODO: modify me
$csvFilePath = "\\SERVER\share\members.csv" # TODO: modify me
$propertiesToExport = @("DisplayName", "Identity", "Alias", "EmailAddresses") # TODO: modify me. If $NULL - return all properties

try
{
    # Create a remote PowerShell session to the Exchange Server
    $session = New-PSSession -configurationname Microsoft.Exchange -connectionURI http://$exchangeServer/PowerShell
    Import-PSSession $session -DisableNameChecking -AllowClobber
    
    # Get the dynamic distribution group
    $group = Get-DynamicDistributionGroup -Identity "%distinguishedName%"
    
    # Get members
    $members = Get-Recipient -RecipientPreviewFilter $group.RecipientFilter -OrganizationalUnit $group.RecipientContainer

    if ($propertiesToExport -ne $NULL)
    {
        $members = $members | Select -Property $propertiesToExport
    }
    
    # Export report
    $members | Export-Csv -NoTypeInformation -Path $csvFilePath
}
finally
{
    # Release resources
    if ($session) { Remove-PSSession $session }
}


Comments ( 0 )
No results found.
Leave a comment