Create Office 365 Group Reports with PowerShell Script (Export CSV)

Create Office 365 Group Reports with PowerShell. In this post, we will be discussing how to create an Office 365 Group report using a Powershell script.

What is an Office 365 Group?

An Office 365 group, which is currently known as a Microsoft 365 group, is an object in Azure Active Directory that holds a list of members that are attached to related Office 365 workloads, including a SharePoint team site, an Exchange mailbox, Planner, One Note, and Power BI.

It is majorly used for collaboration and includes a group email on the creation and gives members of the group access to shared Office 365 resources. You do not need to worry about manually assigning permissions to the shared resources as members automatically have permissions to them.
Office 365 Groups come in two different privacy types, which are mentioned below:
  • Public: This can be joined by users, without getting approval from a group owner. Anyone can access the group content.
  • Private: This is not open for everyone to join, and group owners are the ones that can add members to them. Only members can access the group content.
Users in a Tenant can by default create an Office 365 group from Outlook, and other services are provisioned behind the senses. Collaboration workspaces that are provisioned when an Office 365 group is created differ based on where the group is created.
Below is an image of services being provisioned based on where an Office 365 group is created from:

Types of Office 365 Group Reports

Office 365 Administrators have some routine tasks that could include regularly auditing Office 365 Groups in the tenant.

The auditing of these groups will assist the administrator when cleaning up the tenant, checking for mistakenly created groups by users (if users are not blocked from creating Office 365 groups), during on-boarding and separation of users to update internal documentation.

There are several types of Office 365 Group Reports that administrators run, below are some of them:
  • A report displaying the owner(s) of an Office 365 Group
  • A report displaying the owner(s) of all Office 365 Groups on a tenant
  • A report showing the date of the creation of an Office 365 group
  • A report showing the date of creation of all Office 365 groups on a tenant
  • A report showing the privacy status of an Office 365 group
  • A report showing the privacy status of all Office 365 groups on a tenant
  • A report showing the number of members in an Office 365 group
  • A report showing the number of members in all Office 365 groups on a tenant
  • A report showing the number of Office 365 groups in the tenant
  • A report showing the list of members in an Office 365 group
  • A report showing the list of all Office 365 Groups and their members in a tenant
These reports are mostly generated in CSV or HTML format by the administrators. A CSV report will be of preference in this article.

Generating Office 365 Group Reports using PowerShell

Microsoft provides an option for Office 365 administrators to generate a report for Office 365 groups via the Office 365 Admin portal. The report generated from the Office 365 Admin Portal is a report of all Office 365 groups in the tenant and their attributes.
We will not be making use of the admin portal in this article, instead, we will be working with Windows Microsoft PowerShell and relaying on the Exchange Online Management Module (a.k.a EXO V2 module).
This module can be found at the PowerShell Gallery. This gives us access to create a more detailed report using PowerShell CMDLETs.

Installing the Exchange Online Management Module

To gain access to this module, first, we would have to install the module on the PC we are going to use to generate the report.

Launch the Windows PowerShell in Administrator mode and then run the scripts below:
The first script will be setting the execution policy of the PowerShell Process to Unrestricted. This is necessary to avoid any restrictions.
				
					# set the execution policy of the process
Set-ExecutionPolicy -Scope Process Unrestricted -Force

				
			
The next script will be to install the Exchange Online Management Module.
				
					# Install Exchange Online Module
Install-Module -Name 'ExchangeOnlineManagement' -Force

				
			

Importing the Exchange Online Management Module

Next, we will be importing the installed Exchange Online Management Module into the current PowerShell session using the cmdlet below:
				
					# Import Exchange Online Module
Import-Module -Name 'ExchangeOnlineManagement'-Force

				
			

There are several Office 365 Group reports that we have listed in this article, and we are going to generate a combination of these reports using majorly the Get-UnifiedGroup and Get-UnifiedGroupLinks Exchange Online PowerShell CMDLETs.

  • Get-UnifiedGroup: This cmdlet gets the Office 365 groups in an organization’s Exchange Online environment.
  • Get-UnifiedGroupLinks: This cmdlet gets the membership information for Office 365 Groups in an organization’s Exchange Online environment.

Connecting to Exchange Online

We will first need to connect to Exchange Online to gain access to these CMDLETs using the cmdlets below:

				
					# save tenant admin email in the variable
$AdminUpn = 'admin@contoso.onmicrosoft.com'

# connect to exchange online powershell
Connect-ExchangeOnline -UserPrincipalName $AdminUpn 

				
			

Export Office 365 Group Members to csv using PowerShell

Next, we will be generating an extensive Office 365 Groups report, which will have a combination of the majority of previously listed reports, generated by Office 365 Administrators.
The report will have the following parameters in a CSV output file:
  • Group Display Name
  • Group PrimarySmtpAddress
  • When Created (UTC)
  • Privacy Status
  • Group Member Count
  • Group External Member Count
  • Dynamic MemberShip Status
  • Owners
  • Display Name of Members
  • Members’ PrimarySmtpAddress

Below is the PowerShell script that generates this Office 365 Group member csv report:

Note: Change the $ReportName variable to suit your desired output file path and file name. Also, the “.\” represents the present working directory (you can get this information by running the following cmdlets in your PowerShell session: PWD or Get-Location).

.\” can be changed to any desired directory on the machine (E.g C:\Windows\)

				
					# set the name of the report alongside the path ".\ relative path"
$ReportName = ".\Office365GroupReport.csv"

# get office 365 group
$GroupDetails = Get-UnifiedGroup -ResultSize Unlimited | Select-Object PrimarySmtpAddress, DisplayName, ManagedByDetails, `
WhenCreatedUTC, AccessType, GroupMemberCount, GroupExternalMemberCount, IsMembershipDynamic

# set export array
$ExportArray = @()

# loop through each group
Foreach ($GroupDetail in $GroupDetails) {

    # set the group primary smtp address 
    $GroupUpn = $GroupDetail.PrimarySmtpAddress.ToString()

    # get office 365 group members
    $GroupMembersDetails = Get-UnifiedGroupLinks -ResultSize Unlimited -Identity $GroupUpn -LinkType Members `
    | Select-Object Name, PrimarySmtpAddress

    # add values to array
    $ExportArray += [PSCustomObject][Ordered]@{

        # get office 365 group display name
        "Group Display Name"          = $GroupDetail.DisplayName.ToString()

        # get office 365 group primary smtp address
        "Group PrimarySmtpAddress"    = $GroupUpn

        # get office 365 group creation date
        "When Created (UTC)"          = $GroupDetail.WhenCreatedUTC.ToString()

        # get office 365 privacy status
        "Privacy Status"              = $GroupDetail.AccessType.ToString()

        # get office 365 group member count
        "Group Member Count"          = $GroupDetail.GroupMemberCount.ToString()

        # get office 365 group external member count
        "Group External Member Count" = $GroupDetail.GroupExternalMemberCount.ToString()

        # get dynamic memberShip status
        "Dynamic MemberShip Status"   = $GroupDetail.IsMembershipDynamic.ToString()

        # get office 365 group owner(s)
        "Owners"                      = ($GroupDetail.ManagedByDetails | Out-String).Trim()

        # get the display name of the members
        "Display Name of Members"     = ($GroupMembersDetails.Name | Out-String).Trim()

        # get the primary smtp address of the members
        "Members' PrimarySmtpAddress" = ($GroupMembersDetails.PrimarySmtpAddress | Out-String).Trim()

    }
}

# export to csv file
$ExportArray | Export-Csv -Path $ReportName -Delimiter "," -NoTypeInformation


				
			
After the report gets generated as a CSV file, we will view it using Microsoft Excel and make modifications to achieve a perfect view.

Modifications to Excel Office 365 Group Members CSV Export Report

Once the report is open in Excel, right-click on any column heading and select the Format Cells… option.

Next, click on the Alignment tab, then click on the drop-down menu below the Vertical: option and select Top, then click on the OK button to save.

The above steps will give you a detailed view of the reports in Microsoft Excel as displayed below:

Create Office 365 Group Reports with PowerShell Conclusion

In this article we covered the steps to Create Office 365 Group Reports with our PowerShell script.  Check out our Office 365 reporting tool, that allows you to report on Active Directory and Office 365 users.

Avatar for Esemuede Okougbo
Esemuede Okougbo

My background is in Cloud Operations, Office 365, Exchange Online, Security & Compliance, Active Directory and Dynamics 365 F&O. I'm a PowerShell Developer and ATTUNE Evangelist. Certified Cyber Security Professional (CSFPC).

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x