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.
# 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
Prerequisites:
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:
Get-MgTeam
cmdlet to get the Team ID if you don’t know the ID.Define Output File:
Initialize Array:
Get Team Owners:
Get-MgGroupOwner
cmdlet.Process Each Owner:
Get-MgUser
cmdlet, and add their details to the array.Export Report:
Export-Csv
cmdlet.Disconnect:
Disconnect-MgGraph
command ends the session with Microsoft 365.Send-MailMessage
cmdlet to send the CSV file as an attachment.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!
© m365corner.com. All Rights Reserved. Design by HTML Codex