Use PowerShell to find email aliases

Something I run into all the time is trying to figure out who is holding a specific email address or alias – it can get pretty easy to lose track of aliases, as they don’t show up in your list of users either in the admin center, or in the Exchange Control Panel.

I found this great PowerShell command from the Office 365 Community Forums that allows you to generate a list of all email addresses tied to the mailboxes in your tenancy – here it is:

Get-Mailbox | Select-Object DisplayName,@{Name=”EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_ -LIKE “SMTP:*”}}} | Sort | Export-Csv C:email-aliases.csv

** Note that this script runs against mailboxes, so it will show you the addresses for shared and resource mailboxes, but it won’t tell you what email addresses are used for your distribution groups.

And here is how you run it:

Open PowerShell – the Windows Azure Active Directory Module is required for managing any Office 365 tenancies, so make sure you have it installed:


Log on to your Exchange Control Panel: I’ve found that when trying to manage a tenancy that you have delegated administration rights to, it’s still easiest to log on as the tenant admin (or at least a global admin account), rather than trying to use your delegated account – trying to make the connection jump from your credentials to a client’s never seems to work well for me.

Authenticate: $LiveCred = Get-Credential

102713_1934_UsePowerShe2

Connect to ECP:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection


And then initiate your remote session:

$importresults = Import-PSSession $Session

Once it connects, you can run your script to generate your list of email aliases:

Get-Mailbox | Select-Object DisplayName,@{Name=”EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_ -LIKE “SMTP:*”}}} | Sort | Export-Csv C:email-aliases.csv

 

** Update **

If you have more than 1000 users, you’ll need to include the “ResultSize Unlimited” switch to your command, like so:

Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName,@{Name=”EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_ -LIKE “SMTP:*”}}} | Sort | Export-Csv C:email-aliases.csv

You might want to change the path that you’re saving to, depending on how your system is configured – if you don’t have permission to write to the root of your C:, then just change your path to c:usersyourusernamedesktopemail-aliases.csv

Disconnect your session – when you’re finished, either run the following command, or just close your PowerShell Window – the session will time out and end after a while, but it’s always best practice to close the door when you leave:

Remove-PSSession $Session

That’s it – enjoy!


24 thoughts on “Use PowerShell to find email aliases

      1. Thanks for the command Jeremy on finding e-mail aliases…however, I’m running into an issue because I have more than 1000 entries and It won’t return more than that. It states to use the -ResultSize Unlimited, however that doesn’t work. Any ideas?

        Get-Mailbox | Select-Object DisplayName,@{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_ -LIKE “SMTP:*”}}} | Sort | Export-Csv C:UsersusersDesktopalias.csv
        WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of ite
        ms returned. To return all items, specify “-ResultSize Unlimited”. Be aware that, depending on the actual number of it
        ems, returning all items can take a long time and consume a large amount of memory. Also, we don’t recommend storing t
        he results in a variable. Instead, pipe the results to another task or script to perform batch changes.

        Like

      2. Hey Shawn, if you add the -ResultSize Unlimited right after Get-Mailbox, it should work fine. I updated the command and tried it, and was able to export SMTP addresses for 3100 users. The updated command should look like this:

        Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName,@{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_ -LIKE “SMTP:*”}}} | Sort | Export-Csv C:UsersusersDesktopalias.csv

        Like

  1. I an trying to extract a list of all shared mailbox on office 365 with details “Name , Email, Email aliases” is there any specific command i can runn to extract it on a *.csv file.

    please help me for such command.

    Like

    1. Hi Rama,
      This should do the trick for you:

      Get-Mailbox | Where {$_.RecipientTypeDetails -eq “SharedMailbox”} | Select Name,alias,EmailAddresses | Sort | Export-Csv C:shared-mailboxes.csv -NoTypeInformation

      Like

  2. Hi JEREMY DAHL

    Thank you for the script. Can you please help me to pipe a list of users (SAMACCOUNENAME) via CSV like using import-csv and export-csv the the result.

    Liked by 1 person

    1. Hi Shri,
      I think the problem you’re going to run into is that the SAMACCOUNTNAME field isn’t synchronized into Office 365, and the SAMACCOUNTNAMEs that exist in Office don’t map to your existing account names on-prem. If you can instead use a csv that has your users UPNs, those will properly map into Office 365 (or it should, at least). Does that work for you?

      Let me know what exactly you’re trying to achieve, and I’ll see what I can come up with.

      Thanks,
      Jeremy

      Like

    1. I’m sorry, I’m not sure what you mean – which email addresses are you trying to get at? This script should list all SMTP addresses attached to your users, regardless of domain.

      Like

  3. Hi Jeremy,

    thank you so much for posting this and helping others, this has helped me out many times.

    Nathan

    Like

    1. HI Javi – definitely! You can add a Greater Than (-gt) switch to the filter like so:
      Get-Mailbox | where {($_.EmailAddresses).count -gt 1} | Select DisplayName,@{Name=”EmailAddresses”;Expression={$_.EmailAddresses | Where {$_ -match “SMTP:”}}}

      Something interesting I noticed when running this is that the script will return some email addresses that only have a single SMTP address even though the email address count is greater than 1. This is because those accounts also had a SIP address attached – hopefully this still helps give you what you’re looking for, though!

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.