# 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…

blog

04 Mar 2016 pl

![Microsoft Exchange – Set address book policy based on group membership](/wp-content/uploads/2015/07/PowerShell-Automating-Administration.jpg)

You may remember our older post [how to set address book policy based on group membership](https://evotec.xyz/microsoft-exchange-set-address-book-policy-to-multiple-users-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

```powershell
$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.

```powershell
Get-GlobalAddressList | Update-GlobalAddressList
Get-AddressList | Update-AddressList
Get-OfflineAddressBook | Update-OfflineAddressBook
```

## Powiązane projekty

Projekty powiązane z tagami, kategoriami i treścią tego wpisu.

[Wszystkie projekty](/projekty/)

Trwa dopasowywanie projektów...

[Powrót do bloga](/blog/)

## Powiązane wpisy

- [Check an API write after a PowerShell timeout](/blog/verify-uncertain-api-outcomes-powershell/)
- [Inspect and convert mixed Office archives with PSWriteOffice](/blog/inspect-and-convert-mixed-office-archives-pswriteoffice/)
- [Compose PDF reports and automate page workflows with PSWriteOffice](/blog/automate-pdf-reports-and-page-workflows-pswriteoffice/)

<!-- PowerForge markdown artifact sha256:D91C9AFDF21DCED46F55D5371C877DD76019D7C0BC4E263564D7CACC57F86033 -->
