How to turn off Microsoft Viva daily briefing emails? Every day the users get an…
List Microsoft 365 Groups with PowerShell
You want to list all Microsoft 365 Groups in the tenant. You need that list to gather information because you want to bulk remove alias addresses from the Microsoft 365 Groups. The SMTP with an upper case is the primary email address, and the smtp with a lower case is the secondary email address, also known as the alias address. In this article, you will learn how to list all Microsoft 365 Groups’ primary SMTP and alias SMTP addresses with PowerShell.
Table of contents
Find Microsoft 365 Group email addresses in Microsoft 365 admin center
Sign in to the Microsoft 365 admin center. Click in the Navigation menu on Groups > Active groups. You will see the groups:
- Microsoft 365: Creates a group email to collaborate. You can also add Microsoft Teams for group conversations, files, and calendars.
- Distribution: Creates an email address for a group of people.
- Mail-enabled security: A distribution list that can also be used to control access to OneDrive and SharePoint.
- Security: Controls access to OneDrive and SharePoint and can be used for Mobile Device Management for Microsoft 365.
Click on a Microsoft 365 group. Click Edit under Email addresses.
In the primary email address section, you can edit the primary email address. In the aliases section, you can add, remove and change the alias to primary email. There is one primary email address and three aliases. The alias ending with onmicrosoft.com is by default.
You have seen the Microsoft 365 Group primary email address and aliases in the Microsoft 365 admin center. Clicking on every group and writing down every primary email address and aliases will take time. That’s when you want to use PowerShell and speed up the task.
How to get a list of all the Microsoft 365 Groups, including their primary SMTP and aliases with PowerShell? Read more about it in the next part.
Connect to Exchange Online PowerShell
Before you start, make a connection to Exchange Online PowerShell. Run Windows PowerShell as administrator and connect to Exchange Online PowerShell.
PS C:\> Connect-ExchangeOnline
After connecting, let’s proceed to the next step.
List all Microsoft 365 Groups primary SMTP addresses
Run the cmdlet to list the Microsoft 365 groups with the primary SMTP address.
PS C:\> Get-UnifiedGroup -ResultSize Unlimited | Select-Object DisplayName,PrimarySmtpAddress | Sort-Object DisplayName
DisplayName PrimarySmtpAddress
----------- ------------------
Group1 group1@exoip.com
Group2 group2@exoip.com
In our Microsoft 365 tenant, we have aliases under some Microsoft 365 Groups. In the next part, we will list these aliases.
List all Microsoft 365 Groups primary SMTP addresses and aliases
In the previous part, we listed the primary SMTP address of every Microsoft 365 group in the tenant. Now we like to have a list including the aliases SMTP addresses. How will we add this to the existing PowerShell cmdlet?
We are going to make use of the script block. A script block is a collection of statements or expressions that can be used as a single unit. By using that, it will gather all the smtp aliases. Note that the smtp in the cmdlet is written in lower case letters when we want to get the aliases because it’s not the primary SMTP. The Primary SMTP is written in upper case letters.
PS C:\> Get-UnifiedGroup -ResultSize Unlimited | Select-Object DisplayName,PrimarySmtpAddress, @{Name="Aliases";Expression={($_.EmailAddresses | Where-Object {$_ -clike "smtp:*"} | ForEach-Object {$_ -replace "smtp:",""}) -join "," }} | Sort-Object DisplayName
DisplayName PrimarySmtpAddress Aliases
----------- ------------------ -------
Group1 group1@exoip.com group1new@exoip.com,group1alias@exoip.com,group1@M365x877334.onmicrosoft.com
Group2 group2@exoip.com group2alias2@exoip.com,group2alias1@exoip.com,group2alias@exoip.com,group2@M365x877334.onm...
If you don’t see all the information in the output, make use of the Out-GridView cmdlet.
PS C:\> Get-UnifiedGroup -ResultSize Unlimited | Select-Object DisplayName,PrimarySmtpAddress, @{Name="Aliases";Expression={($_.EmailAddresses | Where-Object {$_ -clike "smtp:*"} | ForEach-Object {$_ -replace "smtp:",""}) -join "," }} | Sort-Object DisplayName | Out-GridView
It will show as below screen.
Export Microsoft 365 Groups to CSV file
Export the results to a CSV file in path C:\temp. In this example, the file name is List_M365_Groups.csv.
PS C:\> Get-UnifiedGroup -ResultSize Unlimited | Select-Object DisplayName,PrimarySmtpAddress, @{Name="Aliases";Expression={($_.EmailAddresses | Where-Object {$_ -clike "smtp:*"} | ForEach-Object {$_ -replace "smtp:",""}) -join "," }} | Sort-Object DisplayName | Export-CSV "C:\temp\List_M365_Groups.csv" -NoTypeInformation -Encoding UTF8
Open the exported CSV file with Microsoft Excel or any other CSV file editor/viewer.
That’s great. I hope that it helped you to export all Microsoft 365 Groups addresses to a CSV file.
Read more: Export AD users to CSV with PowerShell »
Conclusion
In this article, you learned how to list Microsoft 365 Groups with PowerShell. As shown in the article, run the cmdlet to get a list of the Microsoft 365 Groups with SMTP addresses. These are the primary and alias addresses. Export the list to CSV and go through it with Microsoft Excel.
Did you enjoy this article? You may also like the article Remove whitespace from CSV with PowerShell. Don’t forget to follow us and share this article.
This Post Has 0 Comments