User Tools

Site Tools


windows:powershell:fetch_all_exchange_mail_addresses

Once in a while I need to do some Windows management. This time I needed to extract a list of all mail addresses in specific active directory containers. Both the “Primary address” and “Alternate addresses” will be fetched and placed in C:\<country>.txt:

$countrylist = "DK","NO","SE"
$oulist = "user","ExchangeAccounts"

Write-Output "------------------------"
Write-Output "Fetching mail addresses."
Write-Output "------------------------"

foreach ($country in $countrylist) {
	Write-Output "Clearing file C:\$country.txt"
	If (Test-Path C:\$country.txt){
		Clear-Content C:\$country.txt
	}

	foreach ($ou in $oulist) {
		Write-Output "Getting data for $ou in $country and appending it to C:\$country.txt"

		$mailaddr = Get-Mailbox -OrganizationalUnit "ou=$ou,ou=$country,dc=AD,dc=EXAMPLE,dc=COM" -ResultSize Unlimited |Select-Object PrimarySmtpAddress, @{Name="EmailAddresses";Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq "smtp"}}}
		$mailaddr | ForEach-Object {$_.PrimarySmtpAddress} | Add-Content C:\$country.txt
		$mailaddr | ForEach-Object {$_.EmailAddresses -replace("smtp:", "")} | Add-Content C:\$country.txt
	}
}
windows/powershell/fetch_all_exchange_mail_addresses.txt · Last modified: 2015/08/15 22:56 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki