Skip to content

Instantly share code, notes, and snippets.

@Kledenai
Created July 22, 2022 21:10
Show Gist options
  • Save Kledenai/c030b96a94101b34d6cfcc37c61b991b to your computer and use it in GitHub Desktop.
Save Kledenai/c030b96a94101b34d6cfcc37c61b991b to your computer and use it in GitHub Desktop.
# CSV file export path
$Csvfile = "C:\scripts\ExportDGs.csv"
# Get all distribution groups
$Groups = Get-DistributionGroup -ResultSize Unlimited
# Loop through distribution groups
$Groups | ForEach-Object {
$GroupDN = $_.DistinguishedName
$DisplayName = $_.DisplayName
$PrimarySmtpAddress = $_.PrimarySmtpAddress
$SecondarySmtpAddress = $_.EmailAddresses | Where-Object {$_ -clike "smtp*"} | ForEach-Object {$_ -replace "smtp:",""}
$GroupType = $_.GroupType
$RecipientType = $_.RecipientType
$Members = Get-DistributionGroupMember $GroupDN -ResultSize Unlimited
$ManagedBy = $_.ManagedBy
$Alias = $_.Alias
$HiddenFromAddressLists = $_.HiddenFromAddressListsEnabled
$MemberJoinRestriction = $_.MemberJoinRestriction
$MemberDepartRestriction = $_.MemberDepartRestriction
$RequireSenderAuthenticationEnabled = $_.RequireSenderAuthenticationEnabled
$AcceptMessagesOnlyFrom = $_.AcceptMessagesOnlyFrom
$GrantSendOnBehalfTo = $_.GrantSendOnBehalfTo
$Notes = (Get-Group $GroupDN)
# Create objects
[PSCustomObject]@{
DisplayName = $DisplayName
PrimarySmtpAddress = $PrimarySmtpAddress
SecondaryStmpAddress = ($SecondarySmtpAddress -join ',')
Alias = $Alias
GroupType = $GroupType
RecipientType = $RecipientType
Members = ($Members.Name -join ',')
MembersPrimarySmtpAddress = ($Members.PrimarySmtpAddress -join ',')
ManagedBy = $ManagedBy.Name
HiddenFromAddressLists = $HiddenFromAddressLists
MemberJoinRestriction = $MemberJoinRestriction
MemberDepartRestriction = $MemberDepartRestriction
RequireSenderAuthenticationEnabled = $RequireSenderAuthenticationEnabled
AcceptMessagesOnlyFrom = ($AcceptMessagesOnlyFrom.Name -join ',')
GrantSendOnBehalfTo = $GrantSendOnBehalfTo.Name
Notes = $Notes.Notes
}
# Export report to CSV file
} | Sort-Object DisplayName | Export-CSV -Path $Csvfile -NoTypeInformation -Encoding UTF8 #-Delimiter ";"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment