How to export Microsoft 365 Group Owners in One Row using PowerShell

How to Export Microsoft 365 Group Owners in One Row using PowerShell – TrulyBlogs
Export Microsoft 365 Group Owners in One Row using PowerShell | TrulyBlogs

Introduction

Managing Microsoft 365 Groups efficiently is essential for administrators. One common requirement is to get a list of all Microsoft 365 Groups along with their respective owners — all in one row for easier readability and reporting. In this guide, we’ll walk through a simple yet powerful PowerShell script to achieve this.

Prerequisites

Before you begin, ensure you have:
– Installed the Exchange Online PowerShell module
– Connected to Exchange Online
– A CSV file containing Microsoft 365 group email addresses under a column named ‘Kgs’

PowerShell Script to Extract Group Owners

Below is the PowerShell script that reads group email addresses from a CSV file and fetches their owners, presenting each group and its owners in a single row:

PowerShell
# Script to get Microsoft 365 Groups with owners in same row

# Import the CSV file with group email addresses
$groups = Import-Csv -Path "C:\Users\Desktop\GroupsEmailAddress.csv"

# Prepare output array
$output = @()

foreach ($g in $groups) {
    $groupEmail = $g.Kgs
    try {
        # Get group owners
        $owners = Get-UnifiedGroupLinks -Identity $groupEmail -LinkType Owners -ErrorAction Stop |
                  Select-Object -ExpandProperty PrimarySmtpAddress

        # Join owners into single string
        $ownersString = $owners -join ", "

        # Create output object
        $output += [PSCustomObject]@{
            GroupEmail = $groupEmail
            Owners     = $ownersString
        }
    } catch {
        # Handle errors (e.g. group not found)
        $output += [PSCustomObject]@{
            GroupEmail = $groupEmail
            Owners     = "Error: $_"
        }
    }
}

# Export the results to a CSV
$output | Export-Csv -Path "C:\Users\Desktop\information.csv" -NoTypeInformation

Write-Host "Output saved to C:\Users\Desktop\information.csv"

Script Explanation

– The script starts by importing the CSV file containing group email addresses.

– It loops through each email address, retrieving the group owners using `Get-UnifiedGroupLinks`.

– The `Select-Object -ExpandProperty PrimarySmtpAddress` extracts only the SMTP addresses of the owners.

– These addresses are combined into a single string separated by commas using the `-join` operator.

– A custom object with the group email and its owners is created and stored in the `$output` array.

– In case of any error (like a group not found), the script adds an error message for that group.

– Finally, all results are exported to a CSV file located at `C:\Users\Desktop\information.csv`.

Output Example

Here is how the output CSV will look:

GroupEmail,Owners
hr-team@domain.com,john.doe@domain.com, jane.smith@domain.com

Conclusion

This script is a handy tool for Microsoft 365 administrators who need a quick and reliable way to audit group ownership. It saves time, reduces manual work, and ensures that ownership information is readily available in a structured format.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *