Office 365 aliases inventory

As our next example of commonly requested Office 365/Exchange Online reports, we will cover a script that generates a list of all addresses (aliases) in the company. You will find many such examples on the internet, and this script hardly offers anything new. It’s simply an updated version that handles the latest recipient types available in Office 365, as well as support some new alias types.

The core of the script is the Get-Recipient cmdlet, which can be used to return all recipient types we are interested in and (some of) their properties. By default, the script will return only “regular” User mailboxes. As usual, we’ve introduced switch parameters to handle the inclusion of different recipient types, for example the –IncludeSharedMailboxes switch for Shared mailboxes. Since the number of recipient types covered is 15, not all of them get their own switches though. For example, the –IncludeDGs switch will cover all Dynamic Distribution Groups, “regular” Distribution Groups, mail-enabled Security Groups and Room Lists. Similarly, the –IncludeMailUsers switch will cover Mail Users and the recently introduced Guest Mail User objects. Check out lines 33-48 in the script for all the recipient related parameters and their description. You can also use the –IncludeAll switch to cover all supported recipients.

For completeness, X500 aliases are also included. If you want to include other types of aliases, such as the SIP addresses used by Skype for Business or the SPO addresses used by SharePoint Online and the Office Graph, you can specify the corresponding –IncludeSIPAliases and –IncludeSPOAliases switches. In all fairness, you should not care at all about the SPO aliases as they are managed automatically by the service, although we’ve seen some reports about issues with duplicate entries causing trouble. Anyway, another way to include this information in the report is to specify the –IncludeAll switch. Lastly, information about the ExternalEmailAddress attribute is also included for any Mail User or Mail Contact objects.

In order to speed up the script execution and help minimize issues when running it against large environments, we wrap the execution of the Get-Recipient cmdlet via Invoke-Command and only get a minimal set of parameters for each recipient found. For the same reason, we use a workaround in order to include the UserPrincipalName value, as the Get-Recipient cmdlet does not expose this parameter. Instead, we utilize the WindowsLiveID attribute, which should match the UPN for any relevant object type. This saves us from having to run additional cmdlets in order to get the UPN value, and thus greatly reduces the script execution time. Still, it might take substantial amount of time to run the script, something like a minute per each 1000 recipients. Because only a single cmdlet is being executed against Exchange Online, no progress bar is provided this time, so don’t get panicked if nothing seems to happen for a long time 🙂

Lastly, few words about the output. By default, the script will write to a CSV file and also store the output to the global $varAliases variable. You can specify a different location or name for the CSV file, or designate a different variable, by editing the last line of the script. If you do not like the default output, namely one line per each alias, you can use the –CondensedOutput switch to get the “condensed” output, which reduces the CSV file to one line per each recipient instead. Example on the default output is shown below:

The above was generated by running the script with the –IncludeAll switch:

.\O365_aliases_inventory.ps1 -IncludeAll

Additional examples on the script usage can be found in the built-in help. It’s worth mentioning that the script does not include any code to handle Exchange Online connectivity, you will have to take care of this part yourself. If no valid Exchange Online session is detected, then the script will fail.

The script file can be downloaded from the my GitHub repo here: https://github.com/michevnew/PowerShell/blob/master/O365_aliases_inventory.ps1

Should you want to add additional attributes to the script/report, make sure to add them to the list in line 77. As always, don’t hesitate to send us questions or feedback!

This entry was posted in Exchange Online, Microsoft 365, Office 365, PowerShell. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

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