This script will export the members and owners of the DL. We have to give DL name and the CSV file path as an input. It will create an output like below. This script will not give details of Dynamic DL. First 5 columns are for the members details. We can use this script to pull details from Exchange server 2013/2016 and Exchange online. For Exchange online first we have to connect Azure online and then run this script.
$DLName = Read-Host "Enter the DL name"
$DLProp = Get-DistributionGroup $DLName
$Members = Get-DistributionGroupMember $DLname -resultsize unlimited
$output = @()
if($Members.count -eq 0)
{
$Owners = $DLName | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
$Excel = New-Object PSObject
$Excel | Add-Member NoteProperty -Name "DisplayName" -Value EmptyGroup
$Excel | Add-Member NoteProperty -Name "Alias" -Value EmptyGroup
$Excel | Add-Member NoteProperty -Name "RecipientType" -Value EmptyGroup
$Excel | Add-Member NoteProperty -Name "Recipient OU" -Value EmptyGroup
$Excel | Add-Member NoteProperty -Name "Primary SMTP address" -Value EmptyGroup
$Excel | Add-Member NoteProperty -Name "DL Name" -Value $DLName
$Excel | Add-Member NoteProperty -Name "DL Email address" -Value $DLProp.PrimarySmtpAddress
$Excel | Add-Member NoteProperty -Name "DL Managers" -Value $owners.DistributionGroupManagers
$Excel | Add-Member NoteProperty -Name "DL OU" -Value $DLProp.OrganizationalUnit
$Excel | Add-Member NoteProperty -Name "DL Recipient Type" -Value $DLProp.RecipientType
$output += $Excel
}
else
{
Foreach($Member in $members)
{
$Owners = $DLProp | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
write-host $owners
$Excel = New-Object PSObject
write-host $member.name
write-host $dlname
$Excel | Add-Member NoteProperty -Name "DisplayName" -Value $Member.Name
$Excel | Add-Member NoteProperty -Name "Alias" -Value $Member.Alias
$Excel | Add-Member NoteProperty -Name "RecipientType" -Value $Member.RecipientType
$Excel | Add-Member NoteProperty -Name "Recipient OU" -Value $Member.OrganizationalUnit
$Excel | Add-Member NoteProperty -Name "Primary SMTP address" -Value $Member.PrimarySmtpAddress
$Excel | Add-Member NoteProperty -Name "DL Name" -Value $DLName
$Excel | Add-Member NoteProperty -Name "DL Email address" -Value $DLProp.PrimarySmtpAddress
$Excel | Add-Member NoteProperty -Name "DL Managers" -Value $owners.DistributionGroupManagers
$Excel | Add-Member NoteProperty -Name "DL OU" -Value $DLProp.OrganizationalUnit
$Excel | Add-Member NoteProperty -Name "DL Recipient Type" -Value $DLProp.RecipientType
$output += $Excel
}
}
$CSV = Read-Host "Enter the Path of CSV file"
$output | Export-csv -Path $CSV -NoTypeInformation