Office 365 – Powershell: Hide Office/Microsoft 365 Groups from GAL via a Script

Office 365 – Powershell: Hide Office/Microsoft 365 Groups from GAL via a Script

This blog post is applicable for unified groups. However, these groups are also called “Office 365 Groups” or “Microsoft 365 Groups” or simply as “modern groups”.

Unified groups or Office 365 groups are created whenever a new Microsoft Team is created. Apart from this it can also be created separately to serve as a group.

Groups that are created as a result of new Microsoft Teams, are hidden from GAL be default. However, groups that are created via other means like through the portal or via the Outlook panel are not hidden from GAL.

We need to run the following Powershell command to hide them:
Set-UnifedGroup -Identity <> - HiddenFromAddressListsEnabled $true

However, if the requirement is to hide all the groups from GAL, then you have multiple ways to achieve that.

Get-UnifiedGroup -ResultSize unlimited | %{Set-UnifiedGroup -Identity $_.primarysmtpaddress -HiddenFromAddressListsEnabled $TRUE}

In the above command, we extract the list of all unified groups and then use the for-each method to hide each group from GAL. This method does fulfill our purpose; however, if you want to export the list of groups modified in this task, then that would in turn require another command.

If you want this task to be automated, it would be better to write a short script to add all these requirements in one location. This will also make the activity interactive for the person running the script.

The following script does precisely that:

#This script can be used to hide all Microsoft 365 groups (Office 365 groups) from the address list.

#The script is provided as is without any warranties. Please test it in a test environment before using it in production.

#Connect Powershell to Office 365.
$Credentials = Get-Credential
Connect-ExchangeOnline -Credential $Credentials


#Adds all the Office 365 groups to the variable.
$Unified_groups = Get-UnifiedGroup -ResultSize unlimited

#Create an Empty array to add the names of groups that were modified in this script.
$modified_groups = @()

#To get the location to export the lit of modified groups to
$file_location = Read-Host -Prompt "Enter the path where you wish to save the list of modified groups."

#To keep a count of the groups being modified in this script
$modified_counter = 0

foreach ($group in $Unified_groups)
    Write-Host "Working on $group"
    if ($group.HiddenFromAddressListsEnabled -eq $False)
        Write-Host "$group is not hidden from GAL; hence hiding it now." -BackgroundColor DarkMagenta
        Set-UnifiedGroup -Identity $group.Identity -HiddenFromAddressListsEnabled $true
        $modified_groups += $group.PrimarySmtpAddress

        Write-Host "$group is already hidden from GAL." -ForegroundColor Cyan


Write-Host "Number of groups modified: $modified_counter"

#Exports the list of modified groups to the location shared by the user.
$modified_groups >$file_location

#Launches the exported file for your perusal.
notepad $file_location

Here is the link to download the script from Github:

I hope this script helps you to achieve your goal.