Entra / Microsoft 365 · Teams
Hide groups used by teams
Hide the Microsoft 365 Groups used by Teams which might be still visible to Exchange clients and the Exchange address lists.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
# Review required modules and connection steps before running.# Connect to Microsoft Graph or Exchange Online as needed for this script.
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Clear-Host$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}$HiddenGroups = 0Write-Host "Finding team-enabled Microsoft 365 Groups and checking for any which are visible to Exchange clients"[array]$Groups = Get-UnifiedGroup -Filter {ResourceProvisioningOptions -eq "Team"} -ResultSize Unlimited# Reduce to the set visible to Exchange clients[array]$Groups = $Groups | Where-Object {$_.HiddenFromExchangeClientsEnabled -eq $False}# Process the remaining groups and hide them from ExchangeIf ($Groups.Count -ne 0) {ForEach ($Group in $Groups) {Write-Host "Hiding" $Group.DisplayName$HiddenGroups++Set-UnifiedGroup -Identity $Group.ExternalDirectoryObjectId -HiddenFromExchangeClientsEnabled:$True -HiddenFromAddressListsEnabled:$True}} Else {Write-Host "No team-enabled Microsoft 365 Groups are visible to Exchange clients and address lists"}Write-Host ("All done. {0} team-enabled groups hidden from Exchange clients" -f $HiddenGroups)
Attribution
Author
Office365itpros