-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport-ProxyAddresses.ps1
More file actions
86 lines (67 loc) · 2.84 KB
/
Export-ProxyAddresses.ps1
File metadata and controls
86 lines (67 loc) · 2.84 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<#
.SYNOPSIS
Exports all mailbox proxy addresses from Exchange Online to a timestamped CSV or Excel file.
.DESCRIPTION
This script connects to Exchange Online, retrieves all user mailboxes, extracts their primary and proxy SMTP addresses,
and exports them to a file. You can filter the export by domain and choose between CSV or Excel output.
.PARAMETER OutputPath
The directory where the export file should be saved. Defaults to C:\Scripts if not specified.
.PARAMETER DomainFilter
(Optional) A domain string to filter users. Only mailboxes with UPNs ending in this domain will be included.
.PARAMETER OutputExcel
(Switch) If set, the output will be written to an Excel file (.xlsx) using ImportExcel. Otherwise, a CSV is created.
.EXAMPLE
.\Export-ProxyAddresses.ps1 -OutputPath "C:\Exports" -DomainFilter "@contoso.com" -OutputExcel
#>
param(
[string]$OutputPath = "C:\Scripts",
[string]$DomainFilter,
[switch]$OutputExcel
)
# Ensure ExchangeOnlineManagement is imported
if (-not (Get-Module -ListAvailable -Name ExchangeOnlineManagement)) {
Install-Module ExchangeOnlineManagement -Scope CurrentUser -Force
}
Import-Module ExchangeOnlineManagement
# Ensure ImportExcel if needed
if ($OutputExcel -and -not (Get-Module -ListAvailable -Name ImportExcel)) {
Install-Module ImportExcel -Scope CurrentUser -Force
}
if ($OutputExcel) {
Import-Module ImportExcel
}
# Connect to Exchange Online
Connect-ExchangeOnline -ErrorAction Stop
# Get all user mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited
# Prepare array to store results
$proxyAddressesInfo = @()
foreach ($mailbox in $mailboxes) {
if ($DomainFilter -and ($mailbox.UserPrincipalName -notlike "*${DomainFilter}")) {
continue
}
$userProxyAddresses = @($mailbox.PrimarySmtpAddress.ToString())
foreach ($proxy in $mailbox.EmailAddresses) {
if ($proxy.PrefixString -eq "smtp") {
$userProxyAddresses += $proxy.SmtpAddress
}
}
$proxyAddressesInfo += [pscustomobject]@{
DisplayName = $mailbox.DisplayName
UserPrincipalName = $mailbox.UserPrincipalName
ProxyAddresses = ($userProxyAddresses -join "; ")
}
}
# Generate timestamped file name
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$fileNameBase = "ProxyAddresses_$timestamp"
$outputFile = Join-Path -Path $OutputPath -ChildPath ($fileNameBase + $(if ($OutputExcel) { ".xlsx" } else { ".csv" }))
# Export
if ($OutputExcel) {
$proxyAddressesInfo | Export-Excel -Path $outputFile -WorksheetName "ProxyAddresses" -AutoSize
} else {
$proxyAddressesInfo | Export-Csv -Path $outputFile -NoTypeInformation
}
# Disconnect
Disconnect-ExchangeOnline -Confirm:$false
Write-Host "Export completed. Output saved to: $outputFile" -ForegroundColor Green