I am 3-to-4 weeks new to powershell and using it to interact with Active Directory / Exchange. (Both of which I'm also very new to.)

I've tried reading this page: http://technet.microsoft.com/en-us/library/ff730967.aspx but I'm a bit overwhelmed by all of the information, probably because I'm exhausted.

Here's what I'm trying to do. Within my code, I want to take an email address as an input string, and create a mail contact (New-MailContact) with the -ExternalEmailAddress parameter set to this string. This is trivially easy. BUT, if an object in AD already exists with this address, it won't let me create the mail contact. So, before the creation attempt, I need to determine if the call to create-mailcontact will produce an error. (The reason I need to detect this is because if this condition is occurring, I need to run a whole bunch of other commands and logic.)

What I have been trying to do is collect all 'WindowsEmailAddress' properties from the objects returned by these calls:

get-user -resultSize unlimited
get-mailcontact -resultSize unlimited

I then look in these results (I convert them to a nice in-memory hash table where the keys are the lowercase email address strings) to see if the email address I'm going to set up a contact for already exists. For most email addresses that are users in our AD domain, this works great.

Unfortunately, there are a few that don't appear in either of these lists, but I still can't create the mail contact because it says the address already exists:

The proxy address "SMTP:xxxx@xxxx.org" is already being used by
"our.domain.org/Exchange Contacts/Some User Name". Please choose another proxy
  + CategoryInfo          : NotSpecified: (our.domai...xx@xxxx.org:ADObjectId) [New-MailContact], ProxyAddressExistsException
  + FullyQualifiedErrorId : 6580586A,Microsoft.Exchange.Management.Recipient Tasks.NewMailContact

I am a bit afraid to iterate through the list returned by get-recipient... is that what I need to do? I was thinking there is probably an easy way to accomplish this I haven't figured out yet.

Important note: There are about 20-30 addresses, all returning this type of error. HOWEVER, when you look above and see 'Exchange Contacts' in the error, well, that portion of the string is completely different from one faulty email address to the next. Not sure if that's relevant. Please feel free to set me straight on anything I'm not understanding. Thanks!

  • Wouldn't it be easier to just trap the error from New-MailContact with a try/catch block and use the error details to perform your "other commands and logic"? – Ryan Bolger Aug 12 '17 at 0:32

Note that the "mail" attribute is not the only place where an email address may be located. There is also a multi-valued attribute "proxyAddresses", where additional email addresses may be assigned to an object.

Someone has created a script here: (requires the free Quest AD cmdlet)


    Param (
        HelpMessage="Enter SMTP address to search for in Active-Directory."
Function Get-ProxyAddresses ([string]$Address){
$objAD = $null
$objAD = Get-QADObject -LdapFilter "(proxyAddresses=*$Address*)" -IncludeAllProperties -SizeLimit 0 -ErrorAction SilentlyContinue
Write-Output $objAD
}#Close Function
#Validate Quest PSSnapin is loaded
Add-PSSnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
#Run Function to search AD for SMTP address
$Results = $null
$Results = Get-ProxyAddresses -Address $objSMTP | Select-Object Name,DisplayName,ObjectClass,Email,AccountisDisabled,AccountisLockedOut,MailNickName,LegacyExchangeDN -ErrorAction SilentlyContinue
IF($Results -eq $null){
Write-Host ""
Write-Host "No Object Found with .attribute[proxyAddress] containing $objSMTP."}
Else{$Results | Format-List *}
| improve this answer | |


get-recipient "xxxx@xxxx.org"  -ea silentlycontinue

will return results for any mail-enabled object in your exchange environment. This will include contacts, mail-enabled users and mailboxes. My experience is that it should also skim the proxy address fields.

$FindExistingRecipient = get-recipient "xxxx@xxxxx.org" -ea silentlycontinue
if ($FindExistingRecipient -eq $null) {
         new-mailcontact ....
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.