Exchange Online Quick Tip: Export all distribution lists with members to a CSV file!
Hi Microsoft 365 and Exchange Online friends,
This article is about using PowerShell in Exchange Online to discover all distribution lists, including all members. Then export this information to a CSV file.
I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):
#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE
Set-Location C:\Temp
Clear-Host
#We need the module (without the parameter for a specific version)
Install-Module -Name ExchangeOnlineManagement -AllowClobber -Force -Verbose
#Let’s import the module
Import-Module ExchangeOnlineManagement
#Check the version (if you have not selected a version)
Get-InstalledModule -Name ExchangeOnlineManagement
#Update if needed
Update-Module -Name ExchangeOnlineManagement -Verbose
#Now we connect to Exchange Online
Connect-ExchangeOnline
#To list all the distribution groups
Get-DistributionGroup -ResultSize Unlimited
#To obtain a list of group members
Get-DistributionGroupMember -Identity "MarketingTeam20220406114100" -ResultSize Unlimited
#Get members from a given distribution list and then, export its members to a CSV file
$DLName = "MarketingTeam20220406114100"
Get-DistributionGroupMember -Identity $DLName -ResultSize Unlimited |
Select Name, PrimarySMTPAddress, RecipientType |
Export-CSV "C:\Distribution-List-Members.csv" -NoTypeInformation -Encoding UTF8
##Lets have a look
Get-Content C:\Distribution-List-Members.csv | Out-GridView
#Export all distribution lists with members to a CSV file!
$Result=@()
$groups = Get-DistributionGroup -ResultSize Unlimited
$totalmbx = $groups.Count
$i = 1
$groups | ForEach-Object {
Write-Progress -activity "Processing $_.DisplayName" -status "$i out of $totalmbx completed"
$group = $_
Get-DistributionGroupMember -Identity $group.Name -ResultSize Unlimited | ForEach-Object {
$member = $_
$Result += New-Object PSObject -property @{
GroupName = $group.DisplayName
Member = $member.Name
EmailAddress = $member.PrimarySMTPAddress
RecipientType= $member.RecipientType
}}
$i++
}
$Result | Export-CSV "C:\All-Distribution-Group-Members.csv" -NoTypeInformation -Encoding UTF8
#Lets have a look
$Result | Out-GridView
I hope this article was useful. Thank you for taking the time to read the article.
Best regards, Tom Wechsler
P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github! https://github.com/tomwechsler
office365,microsoft
via Microsoft 365 topics https://ift.tt/auwE9Jq
April 6, 2022 at 02:22PM
TomWechsler