-
Notifications
You must be signed in to change notification settings - Fork 8
/
ReportEmailAddressesPerDomain.ps1
52 lines (44 loc) · 1.71 KB
/
ReportEmailAddressesPerDomain.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
###############################################################################
# Report EmailAddresses and PrimaryEmailaddress per AcceptedDomain
# 2023.01.25 - V1.0 - Andres Bohren
###############################################################################
#Connect-ExchangeOnline
If ($Null -eq $(Get-ConnectionInformation))
{
Write-Host "Connect-ExchangeOnline" -ForegroundColor Green
Connect-ExchangeOnline -ShowBanner:$false
}
#Get-AcceptedDomain
Write-Host "Getting AcceptedDomains..." -ForegroundColor Green
$AcceptedDomains = Get-AcceptedDomain
Write-Host "Getting Mailboxes..." -ForegroundColor Green
$Mailboxes = Get-Mailbox -ResultSize Unlimited
#Loop through AcceptedDomains
$Results = @()
$INT = 0
Foreach ($AcceptedDomain in $AcceptedDomains)
{
$INT = $INT + 1
$Domain = $AcceptedDomain.DomainName
Write-Host "Working on Domain: $Domain [$INT]" -ForegroundColor Green
#Additional EmailAddresses
[Array]$RecipientAddress = $Mailboxes| Where-Object {$_.EmailAddresses -like "*@$Domain"}
$RecipientCount = $RecipientAddress.Count
Write-Host "EmailAddressesCount: $RecipientCount"
#PrimaryEmailaddress
[Array]$PrimaryRecipients = $Mailboxes| Where-Object {$_.PrimarySMTPAddress -like "*@$Domain"}
$PrimaryRecipientCount = $PrimaryRecipients.Count
Write-Host "PrimaryAddressCount: $PrimaryRecipientCount"
#Create PSCustomObject
$myObject = [PSCustomObject]@{
Domain = $domain
EmailAddresses = $RecipientCount
PrimaryEmailaddress = $PrimaryRecipientCount
}
#Add to Results Array
$Results += $myObject
}
$Results
$CSVPath = "$PSScriptRoot\EmailAddressesPerDomain.csv"
Write-Host "Exported to $CSVPath"
$Results | Export-Csv -Path "$PSScriptRoot\EmailAddressesPerDomain.csv" -NoTypeInformation