Retrieve Teams Message Settings Using Graph PowerShell

Microsoft Teams has become a cornerstone of modern workplace communication, enabling seamless collaboration across organizations. As an IT administrator, understanding and managing the settings for your teams is crucial to maintaining a productive and secure environment. One important aspect is the control over message settings, such as whether members can edit or delete their messages.

In this article, we’ll walk you through a PowerShell script using Microsoft Graph that allows you to query all teams in your tenant and output these message settings in a clear tabular format.


The Script

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

# Import the module
Import-Module Microsoft.Graph.Teams

# Connect to Microsoft Graph
Connect-MgGraph -Scopes 'Team.ReadBasic.All Group.Read.All'

# Retrieve all Teams in the tenant
$teams = Get-MgTeam -All

# Initialize an array to store the results
$results = @()

foreach ($team in $teams) {
    # Get the messaging settings for each team
    $messageSettings = Get-MgTeam -TeamId $team.Id | Select-Object -ExpandProperty MessagingSettings
    
    # Create an object with the required fields
    $result = [PSCustomObject]@{
        "Team Name"            = $team.DisplayName
        "Description"          = $team.Description
        "User Edit Messages"   = $messageSettings.AllowUserEditMessages
        "User Delete Messages" = $messageSettings.AllowUserDeleteMessages
    }

    # Add the object to the results array
    $results += $result
}

# Output the results in a table format
$results | Format-Table -AutoSize

How the Script Works

This script leverages the Microsoft Graph API to query and retrieve data from your Microsoft Teams environment. Here's a breakdown of how it works:

  • Importing the Module and Connecting to Microsoft Graph: The script begins by importing the Microsoft.Graph.Teams module and establishing a connection to Microsoft Graph. This connection is authenticated using the Connect-MgGraph cmdlet with the necessary permissions, specifically Team.ReadBasic.All and Group.Read.All.
  • Retrieving Teams: The script then fetches all the teams in the tenant using the Get-MgTeam cmdlet. The -All parameter ensures that all teams are retrieved, not just a subset.
  • Fetching Message Settings: For each team, the script retrieves its messaging settings, including whether members can edit or delete their messages.
  • Outputting the Results: The relevant information is stored in a custom object, which is then output in a table format using Format-Table. The table includes the team name, description, and message settings, providing a clear overview of the messaging policies for each team.

Further Enhancing the Script

This script can serve as a foundation for further enhancements depending on your specific needs. Here are a few ideas:

  • Filtering Teams: You can modify the script to only retrieve and display settings for specific teams by adding a Where-Object clause to filter teams based on criteria like name, description, or other properties.
  • Exporting to CSV: If you need to keep a record of the settings or share them with others, you can easily export the output to a CSV file using Export-Csv.
  • Automating the Script: For larger environments, consider scheduling this script to run at regular intervals using Task Scheduler or Azure Automation, ensuring you always have up-to-date information on your teams' message settings.

Possible Errors & Solutions

"Connect-MgGraph: AADSTS65001: The user or administrator has not consented to use the application with ID 'xxxx' named 'Microsoft Graph PowerShell'. Send an interactive authorization request for this user and resource."

Solution: Ensure that you have the necessary permissions and that consent has been granted. You can provide consent by logging in to the Azure portal as an admin and granting the necessary permissions for the Microsoft Graph API.

"Get-MgTeam: The term 'Get-MgTeam' is not recognized as the name of a cmdlet, function, script file, or operable program."

Solution: This error typically occurs if the Microsoft.Graph.Teams module is not installed or not imported correctly. Ensure you have installed the module using Install-Module -Name Microsoft.Graph.Teams and imported it with Import-Module Microsoft.Graph.Teams.

"Insufficient privileges to complete the operation."

Solution: Ensure the account you are using has the appropriate permissions. You may need to adjust the permissions or scope used in the Connect-MgGraph cmdlet. Team.ReadBasic.All and Group.Read.All are the required Graph API permissions.


Conclusion

Managing message settings in Microsoft Teams is vital for ensuring compliance and maintaining control over team communication. By using the script provided in this article, you can quickly and easily retrieve the message settings for all teams in your tenant, helping you to stay informed and make necessary adjustments as needed. Whether you're exporting data for reporting purposes or just performing routine checks, this script offers a flexible and powerful tool to streamline your Teams management tasks.

Remember, the true power of PowerShell lies in its flexibility—feel free to modify and enhance the script to suit your specific requirements. Happy scripting!

Related Articles:

Listing Guest Users in a Microsoft Team Using Graph PowerShell
How to Check for Password Expired Users in Microsoft 365
How to Check for Sign-In Enabled Users in Microsoft 365
How to Remove Users from a Microsoft 365 Group Using PowerShell
Counting Groups in Your Microsoft 365 Tenant
How to Retrieve the Last User Sign-In Date Using Graph PowerShell
How to Get a List of Archived Teams in Microsoft 365 Using Graph PowerShell
Monitoring Guest User Invitations in Microsoft 365 Using Graph PowerShell
How to Retrieve Teams Channel Details in Microsoft 365 Using Graph PowerShell

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