Use PowerShell to find Mail Contacts

It seems like one of the tasks I do the most on projects is discovery and documentation of existing settings in a client’s environment. While there’s a number of reports in the Office 365 portal, I find that nothing beats PowerShell for getting just what I want when I want it – and this time was no exception!

Here’s a quick script you can use to find all the mail contacts in an environment and outputs their name and primary smtp address to a csv file – good for a report, or as step one in a migration, when you can then turn around and use this csv file to create contacts in the new environment.

$mailContacts = @()
$contacts = Get-MailContact -ResultSize Unlimited


foreach ($c in $contacts){

   $mc = New-Object System.Object
   $mc | Add-Member -type NoteProperty -Name Name -Value $c.Name
   $mc | Add-Member -type NoteProperty -Name Email -Value $c.PrimarySmtpAddress
 $mailContacts += $mc

  }

$mailContacts | Export-Csv Mail-contacts.csv -NoTypeInformation

Let me break down what we’re doing here – this command creates an empty array for us to hold our data:

$mailContacts = @()

And then this command does a quick Get to pull all our mail contacts into a variable.

$contacts = Get-MailContact -ResultSize Unlimited

From there, I use a foreach statement to iterate through each contact, and add it to the array we’ve created:


foreach ($c in $contacts){

   $mc = New-Object System.Object
   $mc | Add-Member -type NoteProperty -Name Name -Value $c.Name
   $mc | Add-Member -type NoteProperty -Name Email -Value $c.PrimarySmtpAddress
   $mailContacts += $mc

}

And finally, you can take your variable and display it on the screen, or output it to a csv file using the final command:

$mailContacts | Export-Csv Mail-contacts.csv -NoTypeInformation

If you output it to your screen, this is what it’ll look like:


And here’s the csv output:


I think it might be helpful to break down this array a bit, because it was something I saw the first few times without really understanding what it was doing. When you build an array, you start out with a variable of your choice (in this case, $mc, and use it to create a new PowerShell object. Your next to lines take the same variable and add properties to it by piping the variable into the Add-Member command. You can put as many properties as you like into your array, just keep adding new lines, giving them a unique name, and then populating it with a value that you’ve gotten earlier. You can even use this method for reporting in your scripts, as you can populate these values however you see fit.

At the end of it all, you need to complete your array by using $mailContacts += $mc. This will add a single line on a table with the Name and Email address of each mail contact that you have in your $contacts variable.

There you go – quick and easy, and more than likely the building block for the many different scripts you’ll create over time. Good luck, have fun!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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