For AD SMTP and Alias’
<#
.SYNOPSIS
Export SMTP addresses (primary + secondary) from AD users.
.DESCRIPTION
Uses Get-ADUser to collect the 'proxyAddresses' and 'mail' attributes.
- Primary SMTP is the address with the "SMTP:" prefix (uppercase).
- Other SMTP addresses are those with the "smtp:" prefix (lowercase).
- If proxyAddresses is empty, the script will attempt to use the 'mail' attribute as primary.
.PARAMETER OutputCsv
File path for exported CSV. Default: .\AD-SMTP-Addresses.csv
.PARAMETER SearchBase
Optional AD container (distinguishedName) to restrict the search.
.PARAMETER IncludeDisabled
If specified, include disabled accounts as well. By default disabled accounts are excluded.
.EXAMPLE
.\Export-AD-SMTP.ps1 -OutputCsv C:\temp\smtp-addresses.csv
#>
param(
[string]$OutputCsv = ".\AD-SMTP-Addresses.csv",
[string]$SearchBase,
[switch]$IncludeDisabled
)
# Ensure ActiveDirectory module is available
if (-not (Get-Module -ListAvailable -Name ActiveDirectory)) {
Write-Error "The ActiveDirectory module is not installed or available. Install RSAT/Active Directory module and run again."
exit 1
}
Import-Module ActiveDirectory -ErrorAction Stop
# Build filter
$filter = if ($IncludeDisabled) { { } } else { { Enabled -eq $true } }
# Properties we need
$properties = @("proxyAddresses","mail","distinguishedName","samAccountName","displayName","objectClass")
try {
if ($SearchBase) {
$users = Get-ADUser -Filter * -SearchBase $SearchBase -Properties $properties
} else {
$users = Get-ADUser -Filter * -Properties $properties
}
}
catch {
Write-Error "Failed to query Active Directory: $_"
exit 1
}
$result = foreach ($u in $users) {
# Some objects may not have proxyAddresses (or not be users) - handle safely
$proxy = @()
if ($u.proxyAddresses) {
$proxy = $u.proxyAddresses
}
# Normalize and split SMTP addresses
$primary = $null
$others = @()
if ($proxy.Count -gt 0) {
# find exact uppercase SMTP: for primary
$primaryEntry = $proxy | Where-Object { $_ -like "SMTP:*" } | Select-Object -First 1
if ($primaryEntry) {
$primary = $primaryEntry -replace '^[sS][mM][tT][pP]:','' # remove prefix (case-insensitive)
} else {
# no uppercase SMTP found -> try any smtp: entry as fallback
$fallback = $proxy | Where-Object { $_ -match '^(smtp|SMTP):' } | Select-Object -First 1
if ($fallback) { $primary = $fallback -replace '^[sS][mM][tT][pP]:','' }
}
$others = $proxy |
Where-Object { $_ -match '^(smtp|SMTP):' } |
Where-Object { ($_ -replace '^[sS][mM][tT][pP]:','') -ne $primary } |
ForEach-Object { $_ -replace '^[sS][mM][tT][pP]:','' }
}
# If no proxyAddresses, fall back to mail attribute as primary (if present)
if (-not $primary -and $u.mail) {
$primary = $u.mail
}
# Build object for export
[PSCustomObject]@{
DistinguishedName = $u.DistinguishedName
SamAccountName = $u.SamAccountName
DisplayName = $u.DisplayName
ObjectClass = $u.ObjectClass
PrimarySMTP = $primary
OtherSMTPs = if ($others.Count -gt 0) { $others -join ";" } else { $null }
}
}
# Export to CSV
try {
$result | Export-Csv -Path $OutputCsv -NoTypeInformation -Encoding UTF8
Write-Output "Export completed: $OutputCsv ('$($result.Count) records)'"
}
catch {
Write-Error "Failed to export CSV: $($_)"
exit 1
}