Get Microsoft Teams Owners with Graph PowerShell

Managing and auditing Microsoft Teams ownership is a crucial task for administrators. Automating this process with Graph PowerShell can significantly streamline your administration tasks. Below is a detailed script to fetch and generate a report of all owners of a specific Microsoft Team, including their Display Name and User Principal Name.


PowerShell Script

# Install the Microsoft.Graph module if not already installed
Install-Module -Name Microsoft.Graph -Force -AllowClobber

# Connect to Microsoft 365
Connect-MgGraph -Scopes "Group.Read.All" "User.Read.All"

# Define the Team ID
$teamId = "4a6c54df-9235-4854-8b98-5c0045c02855" # Replace with your specific Team ID

# Define the output CSV file path
$outputFile = "C:\Reports\TeamOwnersReport.csv"

# Initialize an array to store owner details
$teamOwners = @()

# Get the owners of the team
$owners = Get-MgGroupOwner -GroupId $teamId -All

# Process each owner
foreach ($owner in $owners) {
    $user = Get-MgUser -UserId $owner.Id -Property DisplayName, UserPrincipalName
    
    $ownerDetails = [PSCustomObject]@{
        DisplayName      = $user.DisplayName
        UserPrincipalName = $user.UserPrincipalName
    }
    $teamOwners += $ownerDetails
}

# Export the report to a CSV file
$teamOwners | Export-Csv -Path $outputFile -NoTypeInformation

Write-Output "Team owners report generated: $outputFile"

# Disconnect from Microsoft 365
Disconnect-MgGraph

Script Explanation

Prerequisites:

  • Ensure the Microsoft.Graph module is installed and connected to your Microsoft 365 tenant.
  • The Connect-MgGraph command initiates a connection to Microsoft 365 with the required scopes for reading group and user information, namely Group.Read.All and User.Read.All.

Define Team ID:

  • Specify the Team ID of the Microsoft Team you want to retrieve the owners for. Replace "your-team-id-here" with the actual Team ID. Use Get-MgTeam cmdlet to get the Team ID if you don’t know the ID.

Define Output File:

  • Specify the path for the output CSV file where the team owners report will be saved.

Initialize Array:

  • Initialize an array to store owner details.

Get Team Owners:

  • Retrieve all owners of the specified Microsoft Team using the Get-MgGroupOwner cmdlet.

Process Each Owner:

  • Loop through each owner, retrieve their details using the Get-MgUser cmdlet, and add their details to the array.

Export Report:

  • Export the array of team owner details to a CSV file at the specified path using the Export-Csv cmdlet.

Disconnect:

  • The Disconnect-MgGraph command ends the session with Microsoft 365.

Enhancements

  • Add More Owner Details: Include additional owner properties such as Job Title or Department in the report. For example, add JobTitle and Department properties to the $user query and include them in the custom object.
  • Filter Owners: Add filters to generate reports for specific attributes of the owners, such as those from a specific department. Modify the script to include filtering logic before adding details to the array.
  • Schedule the Script: Schedule the script to run at regular intervals using Windows Task Scheduler or Azure Automation to keep the report updated. This ensures you always have the latest information on team owners.
  • Email Report: Add functionality to email the report to administrators once it is generated. You can use the Send-MailMessage cmdlet to send the CSV file as an attachment.

Conclusion

Automating the retrieval of Microsoft Teams owners using Graph PowerShell simplifies administrative tasks, saving time and reducing the potential for human error. This script provides a straightforward way to generate comprehensive reports on team ownership, ensuring you have up-to-date information on who has ownership roles within your Microsoft Teams.

By further enhancing the script, you can add more functionalities and make it an even more powerful tool in your administrative toolkit. Embrace automation and streamline your Microsoft 365 management with the power of Graph PowerShell!


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex