Exchange

Microsoft Exchange – Set address book policy based on group membership

You may remember our older post how to set address book policy based on group membership. After using this method above for a while we've found there is a far easier solution to get group membership with a recursive option on newer systems. We've also updated setting address book policy method with few more checks so it's a bit more error proof.

✅ Solution

So instead of code from above link you can simply use this one

$global:logPlace = "C:\ExchangeScripts\Automated\ExchangeService.log"

function Global-LogInfo ([string] $logFile, [string] $msg, [string] $color = "Blue", [string] $backgroundColor = "White", [boolean] $showOnly = $false) { 
   $timeFormat = "yyyy-MM-dd HH:mm:ss" # https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
   if ($showOnly -eq $true) {
        Write-Host "[$([datetime]::Now.ToString($timeFormat))]$msg" -ForegroundColor $color # -BackgroundColor $backgroundColor
   } else {
        Write-Host "[$([datetime]::Now.ToString($timeFormat))]$msg" -ForegroundColor $color # -BackgroundColor $backgroundColor
        if ($logFile -eq $null) {

        } else {
            Write-Output "[$([datetime]::Now.ToString($timeFormat))]$msg" | Out-File $logFile -Encoding utf8 -Append
        }
  }
}
function Get-GroupMembershipRecursive ($group) {
   $AllMembers = Get-ADGroupMember $group -Recursive | Get-ADUser -Properties DisplayName, Mail,msExchHomeServerName -ErrorAction SilentlyContinue | Select Name,msExchHomeServerName,ObjectClass,Mail, DisplayName
   Write-Output $AllMembers
}
function SetAddressBookPolicy ([string] $AddressBookPolicy, [string] $SetOnGroup, [Boolean] $debug = $false) {    
    $members = Get-GroupMembershipRecursive $SetOnGroup
    foreach ($member in $members) {
        #Global-LogInfo -logFile $global:logPlace -msg "[+] Information: $($member.Name) with email $($member.Mail) [+]"            
        if ($($member.Mail) -ne $null -and $($member.msExchHomeServerName) -ne $null) {
            $mailbox = Get-Mailbox $member.Mail | Select AddressBookPolicy 
            if ($($mailbox.AddressBookPolicy)-eq $AddressBookPolicy) {
                if ($debug -eq $true) {
                    Global-LogInfo -logFile $global:logPlace -msg "[+] Already set $AddressBookPolicy on $($member.Name) address. Skipping. [+]"
                }
            } else {
                
                Global-LogInfo -logFile $global:logPlace -msg "[+] Setting $AddressBookPolicy on $($member.Mail) address. Replaced address book policy: $($mailbox.AddressBookPolicy) [+]"               
                Set-Mailbox $member.Mail -AddressBookPolicy $AddressBookPolicy
            }
        } else {
            if ($debug -eq $true) {
                Global-LogInfo -logFile $global:logPlace -msg "[+] Skipping $AddressBookPolicy on $($member.Name) address. [+]"
            }
        }
    }

}

Remember to use following commands to make sure the Global Address List, Address Lists and Offline Address Book are all up to date.

Get-GlobalAddressList | Update-GlobalAddressList
Get-AddressList | Update-AddressList
Get-OfflineAddressBook | Update-OfflineAddressBook

This post was last modified on 6 czerwca, 2025 21:18

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Supercharging Your Network Diagnostics with Globalping for NET

Ever wondered how to run network diagnostics like Ping, Traceroute, or DNS queries from probes…

6 dni ago

Automating Network Diagnostics with Globalping PowerShell Module

Are you tired of manually running network diagnostics like Ping, Traceroute, or DNS queries? The…

7 dni ago

Enhanced Dashboards with PSWriteHTML – Introducing InfoCards and Density Options

Discover new features in the PSWriteHTML PowerShell module – including New-HTMLInfoCard, improved layout controls with…

2 tygodnie ago

Mastering Active Directory Hygiene: Automating SIDHistory Cleanup with CleanupMonster

Security Identifier (SID) History is a useful mechanism in Active Directory (AD) migrations. It allows…

2 tygodnie ago

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

2 tygodnie ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

2 tygodnie ago