-
Notifications
You must be signed in to change notification settings - Fork 15
/
Export_ALL_Distribution_List_Members_with_Membership.ps1
57 lines (45 loc) · 1.89 KB
/
Export_ALL_Distribution_List_Members_with_Membership.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#The first two lines set the location and clear the shell content.
Set-Location C:\
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