Skip to content

Script to Update 'Hide from Address List' Attribute for Active Directory Users Based on CSV Input

Update ‘Hide from Address List’ Attribute Using CSV Input

Section titled “Update ‘Hide from Address List’ Attribute Using CSV Input”

This script imports a CSV file containing mailbox data and matches each mailbox with a corresponding Active Directory (AD) user based on their primary SMTP address. For each matched user, it updates the msExchHideFromAddressLists attribute, controlling whether the mailbox is hidden from the Global Address List (GAL). The script checks for scenarios like no matching AD user or multiple matches and handles any errors encountered during the update process.


Ideal for: Exchange administrators managing GAL visibility for mailboxes

FeatureCapabilityBusiness Value
CSV-Based ProcessingImports mailbox data from CSV filesBatch processing capabilities
GAL Visibility ControlUpdates msExchHideFromAddressLists attributeManage address list visibility
User Matching LogicMatches mailboxes to AD users via emailAccurate user identification
Error HandlingComprehensive error logging and reportingTroubleshooting and audit trail

Terminal window
$ExcludeDomain = "XXX.onmicrosoft.com"
$CSVPath = "$home\desktop\csvfile.csv"
$AllMailboxs = Import-Csv -Path $CSVPath
# Get all Active Directory users
$AllADUsers = Get-ADUser -Filter * -Property "msExchHideFromAddressLists"
# Loop through each mailbox
foreach ($Mailbox in $AllMailboxs) {
    # Find the corresponding AD user based on the mailbox's primary SMTP address
    $ADUser = $AllADUsers | Where-Object EmailAddress -eq $($Mailbox.EMAILFROMAD)
    # Handle different scenarios based on the number of matching AD users
    switch ($ADUser.count) {
        0 { Write-Warning "No AD User found with $($Mailbox.EMAILFROMAD) E-mail address" }
        {$_ -gt 1} { Write-Warning "Multiple AD Users found with $($Mailbox.EMAILFROMAD) E-mail address" }
        1 {
            try {
                $hash = @{msExchHideFromAddressLists ="$($Mailbox.HIDEGAL)"}
                $ADUser | -Replace $hash -ErrorAction Stop
                Write-Output "Updated $($Mailbox.EMAILFROMAD) Hide from GAL Attribute Successfully"
            }
            catch {
                Write-Output "Failed to update $($Mailbox.EMAILFROMAD) Hide from GAL Attribute. Error: $($_.Exception.Message)"
            }
         }
    }
}

ComponentRequirementPurpose
Active Directory ModuleActiveDirectory PowerShell moduleAccess AD user attributes
CSV Input FileFormatted mailbox dataSource for GAL visibility settings
AD Admin RightsUser attribute modification permissionsUpdate msExchHideFromAddressLists
ColumnDescriptionRequired
EMAILFROMADPrimary SMTP address for user matchingYes
HIDEGALBoolean value for GAL visibility ($true/$false)Yes

  1. CSV Data Import

    • Load mailbox data from specified CSV file
    • Validate required columns exist
  2. AD User Retrieval

    • Get all AD users with GAL visibility attribute
    • Prepare for matching process
  3. User Matching

    • Match mailboxes to AD users via email address
    • Handle multiple match scenarios
  4. Attribute Updates

    • Update msExchHideFromAddressLists based on CSV data
    • Log success/failure for each operation

ScenarioResponseResolution
No AD User FoundWarning loggedVerify email address accuracy
Multiple AD UsersWarning loggedReview duplicate accounts
Update FailureError loggedCheck permissions and data integrity
CSV Format IssuesScript failureValidate CSV structure and data

EMAILFROMAD,HIDEGAL
user1@contoso.com,$true
user2@contoso.com,$true
EMAILFROMAD,HIDEGAL
user3@contoso.com,$false
user4@contoso.com,$false

ParameterExampleDescription
ExcludeDomain"contoso.onmicrosoft.com"Domain to exclude from processing
CSVPath"$home\desktop\mailboxes.csv"Path to input CSV file

Key Takeaway: This script provides efficient batch management of GAL visibility settings through CSV-based processing, enabling administrators to quickly update address list visibility for multiple mailboxes while maintaining comprehensive error tracking and logging.