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.
Script Overview
Section titled “Script Overview”Ideal for: Exchange administrators managing GAL visibility for mailboxes
Key Features
Section titled “Key Features”| Feature | Capability | Business Value |
|---|---|---|
| CSV-Based Processing | Imports mailbox data from CSV files | Batch processing capabilities |
| GAL Visibility Control | Updates msExchHideFromAddressLists attribute | Manage address list visibility |
| User Matching Logic | Matches mailboxes to AD users via email | Accurate user identification |
| Error Handling | Comprehensive error logging and reporting | Troubleshooting and audit trail |
Script Implementation
Section titled “Script Implementation”$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)" } } }}Configuration Requirements
Section titled “Configuration Requirements”Prerequisites
Section titled “Prerequisites”| Component | Requirement | Purpose |
|---|---|---|
| Active Directory Module | ActiveDirectory PowerShell module | Access AD user attributes |
| CSV Input File | Formatted mailbox data | Source for GAL visibility settings |
| AD Admin Rights | User attribute modification permissions | Update msExchHideFromAddressLists |
CSV File Structure
Section titled “CSV File Structure”| Column | Description | Required |
|---|---|---|
| EMAILFROMAD | Primary SMTP address for user matching | Yes |
| HIDEGAL | Boolean value for GAL visibility ($true/$false) | Yes |
Script Logic Flow
Section titled “Script Logic Flow”Processing Steps
Section titled “Processing Steps”-
CSV Data Import
- Load mailbox data from specified CSV file
- Validate required columns exist
-
AD User Retrieval
- Get all AD users with GAL visibility attribute
- Prepare for matching process
-
User Matching
- Match mailboxes to AD users via email address
- Handle multiple match scenarios
-
Attribute Updates
- Update
msExchHideFromAddressListsbased on CSV data - Log success/failure for each operation
- Update
Error Handling Scenarios
Section titled “Error Handling Scenarios”| Scenario | Response | Resolution |
|---|---|---|
| No AD User Found | Warning logged | Verify email address accuracy |
| Multiple AD Users | Warning logged | Review duplicate accounts |
| Update Failure | Error logged | Check permissions and data integrity |
| CSV Format Issues | Script failure | Validate CSV structure and data |
Usage Examples
Section titled “Usage Examples”1. Hide Mailboxes from GAL
Section titled “1. Hide Mailboxes from GAL”EMAILFROMAD,HIDEGALuser1@contoso.com,$trueuser2@contoso.com,$true2. Show Mailboxes in GAL
Section titled “2. Show Mailboxes in GAL”EMAILFROMAD,HIDEGALuser3@contoso.com,$falseuser4@contoso.com,$falseConfiguration Parameters
Section titled “Configuration Parameters”| Parameter | Example | Description |
|---|---|---|
| ExcludeDomain | "contoso.onmicrosoft.com" | Domain to exclude from processing |
| CSVPath | "$home\desktop\mailboxes.csv" | Path to input CSV file |
Conclusion
Section titled “Conclusion”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.