Skip to main content

Managing email addresses with PowerShell

By April 8, 2023No Comments

One of the most common tasks as an administrator is to find, change or add an email address to a user mailbox.  PowerShell makes this tasks a lot easier, you can easily find the users where the e-mail address belongs to and add or remove an alias.

In this article I will show you some tips howto find an e-mail address or ProxyAddress in Exchange or Active Directory and how to add or remove it.

Finding the owner of the e-mail address

I am working with Exchange Online here, so first we need to connect to Exchange Online.  You can use this connector script or run the following code:

1.  #Create credential object
2.  $credObject = Get-Credential
4.  #Import the Exchange Online ps session
5.  $ExchOnlineSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $credObject -Authentication Basic -AllowRedirection
6.  Import-PSSession $ExchOnlineSession

There are multiple ways to find the owner of an email address, we start with the get-recipient cmdlet. This will return all mail-enabled objects (Mailboxes, mail users, contacts, distribution groups) , so this is the best place to start your search.

By using filters and wildcard we can search on a part of the email address:

1.  Get-Recipient -Filter {EmailAddresses -like 'smtp:gr*' -or EmailAddresses -like 'gr*'}

In this case we are search for an email address that starts with “gr” (note the * wildcard at the end). I added “smtp:” before the start of the mail address, because if your aliases are managed through Active Directory, you will note that they start with “smtp:”.

Of course you can also add a wildcard in front of it, but this will widen the search query a lot.

The command above will return the user of object that has the searched name or letters in the email address, but it won’t show where. So select the EmailAddress field and expand it to show all values of the multi value object.

1.  Get-Recipient -Filter {EmailAddresses -like 'smtp:gr*'} -Properties Name,Emailaddresses | select Name,Emailaddresses -ExpandProperty Emailaddresses

Searching Active Directory Objects

If you want to check or find the email address of an Active Directory object (user or group), we can use the get-ADObject or get-aduser cmdlet. First we need to import the ActiveDirectory module:

1.  Import-Module ActiveDirectory

We can use the cmd below to search in Active Directory.  We will filter the results based on the mail address or proxyAddress. By using a wildcard and the -like switch we can search for a part of the email address.

1.  Get-ADObject -Properties mail, proxyAddresses -Filter {mail -like "gr*" -or proxyAddresses -like "smtp:gr*"}

Add or Remove a proxyAddress to an AD user

With PowerShell we can also easily add or remove a proxyaddress to an user:

1.  Set-ADUser -Identity user1 -Add @{Proxyaddresses=""}

Removing it:

1.  Set-ADUser -Identity user1 -Remove @{Proxyaddresses=""}