# Specify the path where you want to save the CSV file
$csvPath = "C:\exported_users.csv"
# Retrieve all users from Active Directory with specific attributes
$users = Get-ADUser -Filter * -Properties SamAccountName, mail, proxyAddresses |
Select-Object SamAccountName, @{Name="PrimarySMTP"; Expression={$_.mail}},
@{Name="SecondarySMTP"; Expression={$_.proxyAddresses -replace '^smtp:(.+)', '$1' -join ";"}}
# Export the users to CSV
$users | Export-Csv -Path $csvPath -NoTypeInformation
Write-Host "Export completed. CSV file saved to: $csvPath"