Entra / Microsoft 365 · Teams
Set auto reply teams groups
Sets an auto-reply message on team-enabled Microsoft 365 groups using Graph and Exchange Online PowerShell.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -Scopes Directory.Read.All, ChannelSettings.Read.All, Team.ReadBasic.All -NoWelcomeConnect-ExchangeOnline
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Clear-Host# Check that we are connected to Exchange Online$ModulesLoaded = Get-Module | Select-Object NameIf (!($ModulesLoaded -match "ExchangeOnlineManagement")) {Write-Host "Please connect to the Exchange Online Management module and then restart the script"; break}# Check that we're connected to the Microsoft Graph and if not, connect...$Status = (Get-MgContext).AccountIf (!($Status)) {Connect-MgGraph -Scopes Directory.Read.All, ChannelSettings.Read.All, Team.ReadBasic.All -NoWelcome}Write-Host "Finding team-enabled Microsoft 365 Groups..."[array]$Teams = Get-MgTeam -AllIf (!($Teams)) {Write-Host "No teams found - exiting"; break} Else {$Teams = $Teams.Value}Write-Host ("{0} team-enabled groups found... now to check for email-enabled channels." -f $Teams.count)$Report = [System.Collections.Generic.List[Object]]::new()ForEach ($Team in $Teams) { # Need to do it this way because Get-MgTeamChannel returns the group email address for the General channelWrite-Host "Processing" $Team.DisplayName$Uri = "https://graph.microsoft.com/v1.0/teams/$($team.id)/channels"[array]$Channels = Invoke-MgGraphRequest -Uri $Uri -Method GetForEach ($Channel in $Channels.Value) {If (!([string]::IsNullOrWhiteSpace($Channel.Email))) {$ReportLine = [PSCustomObject]@{Team = $Team.DisplayNameChannel = $Channel.DisplayNameEmail = $Channel.EmailTeamId = $Team.Id }$Report.Add($ReportLine) }} # End for each channel} # End for each team[array]$GeneralChannels = $Report | Where-Object {$_.Channel -eq "General"}ForEach ($Team in $Teams) {$EmailAddress = $Null$EmailAddress = $GeneralChannels | Where-Object {$_.TeamId -eq $Team.Id}| Select-Object -ExpandProperty EmailIf ($EmailAddress) {Write-Host "Setting auto reply with mailto: for" $Team.DisplayName$InternalMessage = 'Please! We use Teams for communication, so send your message to <a href = "mailto:' + $EmailAddress + '">Teams</a> and it will be dealt with there.'Set-MailboxAutoReplyConfiguration -Identity $Team.Id -ExternalMessage "Sorry, this mailbox doesn't accept email" `-AutoReplyState Enabled -InternalMessage $InternalMessage} Else {Write-Host ("Using default auto-reply for {0}" -f $Team.DisplayName)Set-MailboxAutoReplyConfiguration -Identity $Team.Id `-ExternalMessage "Sorry, this mailbox doesn't accept email" -ExternalAudience All `-AutoReplyState Enabled -InternalMessage "Please use Teams to communicate with us"}}# End For each team# Comment these lines out if you don't want the script to create a temp directory to store its output files$path = "C:\Temp"If(!(test-path $path)) {New-Item -ItemType Directory -Force -Path $path | Out-Null }$Report | Export-CSV -NoTypeInformation c:\temp\ListofEmailEnabledTeamChannels.csvWrite-Host "A listing of email-enabled teams channels is in c:\temp\ListofEmailEnabledTeamChannels.csv"
Attribution
Author
Office365itpros