This one comes up quite often, so I figured I’d make a quick and easy script for it. The first step is to make sure you’re on a machine with the Exchange Management Tools installed, and logged on as a user with privileges to read mailbox properties. If you don’t want to read the explanation, skip ahead to The Script. First, let’s create an empty array to store the results in:
Next, we’ll grab all the mailboxes from the organisation. By default Exchange will only return the first 1000 results, so we need to specify the
ResultSize parameter to ensure all mailboxes are counted.
Now loop through each mailbox using a
Per each mailbox, let’s loop through the email addresses. Exchange stores SMTP, X400, SIP and any number of other address types in this property. We’re only interested in SMTP addresses, so we’ll use an if statement inside this loop. Because Exchange may qualify the address with either
smtp: we’ll convert the prefix to lower and then do the compare:
Doing this has allowed us to only inspect SMTP addresses. Now we can create an object and insert the appropriate variables in it. Finally, we’ll append it to the
$addresses array we created earlier. We’ll use SubString to retrieve only characters after the 5th position from the email address (
smtp:[email protected] will become
Now we have an object,
$addresses which we can export and/or format in any number of ways. In my example below, I have exported it to CSV then opened the CSV (using
Invoke-Item) with its default handler. You could also use Format-Table, Format-List and any number of different methods. You can even interact with this array in other cmdlets or pass it down the pipeline. The possibilities are endless!
There's no commenting functionality here. If you'd like to comment, please either mention me (@[email protected]) on Mastodon or email me. I don't have any logging or analytics running on this website, so if you found something useful or interesting it would mean a lot to hear from you.