Exchange admins frequently need to find an Exchange recipient with a specified email address, particularly for generic organizational addresses such as [email protected]. Five and a half ways to find an email address in Microsoft Exchange and Active Directory lists a few ways to do it, including PowerShell.
If you do this frequently, you can add it as a function to your PowerShell profile. Open your profile in a text editor or PowerShell ISE, and add the following.
-
1. Check if a remote Powershell session exists
The first step is to create a Remote PowerShell session to Exchange Online or your on-premises Exchange Server. But before you do that, it makes sense to check if a session already exists. I use the EXO function to connect to Exchange Online, and the EXOSessionExists function to check if there’s an existing session. If a session doesn’t exist, it creates one.
You can use it for any other scripts or functions that require a RPS session to Exchange Online or Exchange Server. Add them to your PowerShell profile.
1 2 3 4 5 6
function EXO { Import-PSSession (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential (Get-Credential) -Authentication Basic -AllowRedirection) } function EXOSessionExists { if (!(Get-PSSession | Where { $_.ConfigurationName -eq "Microsoft.Exchange" -and $_.computername -like "outlook.office365.com" })) {EXO} }
See Connect to Exchange Online using a PowerShell function for more info on these functions.
For on-premises Exchange Server, you can replace outlook.office365.com with your Exchange Server FQDN or remove the condition completely (-and $_.computername -like “outlook.office365.com”).
-
2. Find an email address using the Find-EmailAddress function
The next step is to actually search the email address specified as commandline inputs. Add this to your PowerShell profile.
1 2 3 4 5 6 7 8 9
Function Find-EmailAddress { param ( [Parameter(Position=0, Mandatory=$TRUE, ValueFromPipeline=$TRUE) ] [array] $EmailAddresses) EXOSessionExists foreach ($emailaddress in $EmailAddresses) { write-host "`nFinding: $emailaddress `n" $objRecipient = get-recipient | where {$_.emailaddresses -match "$emailaddress"} write-host "Recipient:" $objRecipient.DisplayName;write-host "Email addresses:";write-host "Type:" $objRecipient.RecipientTypeDetails;$objRecipient.emailaddresses } }
Now, you can find a recipient with an email address using the following command, and it takes multiple values too:
Find-EmailAddress -EmailAddresses “[email protected]”,”[email protected]”
List all email addresses for a recipient
Another frequent Exchange task is to check all email addresses for a user, distributiong group, contact, etc. This is a fairly easy one-liner – (Get-Recipient “Bharat Suneja”).EmailAddresses.
If you use this frequently, you can add the Get-EmailAddresses function to your Powershell profile:
1 2 3 4 5 6 7 | Function Get-EmailAddresses { param ( [Parameter(Position=0, Mandatory=$TRUE, ValueFromPipeline=$TRUE) ] [array] $Recipients) EXOSessionExists foreach ($recipient in $Recipients) { $objRecipient=Get-Recipient $recipient write-host "`nRecipient:" $objRecipient.DisplayName "`nType:" $objRecipient.RecipientTypeDetails "`nPrimary address:" $objRecipient.primarysmtpaddress "`nEmail addresses:";$objRecipient.emailaddresses} } |
This also requires the EXOSessionExists function mentioned in Step 1 above.
Now, you can list all email addresses for a recipient using the following command, and it takes multiple values too:
Get-EmailAddresses -Recipients “Recipient1″,”Recipient2”
Download Find-EmailAddress.txt.
{ 0 comments… add one now }