During the deployment of a system that supports LDAP synchronization, I noticed that many users were successfully synced but had no email address associated with their accounts.
After checking Active Directory, I found that the issue wasn’t with the LDAP configuration at all. Instead, many user objects simply had an empty mail attribute.
This turned out to be a historical issue. Over the years, AD accounts had been created manually by different IT staff members. Since neither Windows logon nor Exchange depends on the mail attribute, users could work normally even if it was left blank, so the missing values went unnoticed for a long time.
Fortunately, our organization follows a consistent email naming convention, for example:
<sAMAccountName>@yourdomain.comWith a predictable naming convention, it’s easy to populate the missing mail attributes automatically using PowerShell.
PowerShell Script
# Load Active Directory module if not already loadedImport-Module ActiveDirectory
# Define the group name$groupName = "changeme"
# Process users and update missing emailsGet-ADGroupMember -Identity $groupName -Recursive | Where-Object { $_.objectClass -eq 'user' } | ForEach-Object { $user = Get-ADUser -Identity $_ -Properties mail
if (-not $user.mail) { $email = "$($user.SamAccountName)@yourdomain.com" Set-ADUser -Identity $user.SamAccountName -EmailAddress $email Write-Host "Set email for $($user.SamAccountName): $email" -ForegroundColor Green } else { Write-Host "Email already set for $($user.SamAccountName): $($user.mail)" -ForegroundColor Yellow } }What the Script Does
The script performs the following actions:
- Retrieves all users from the specified AD group, including nested groups.
- Checks whether the mail attribute is empty.
- If the attribute is missing, generates an email address using the user’s sAMAccountName.
- Writes the generated email address back to the mail attribute in Active Directory.
- Skips users who already have an email address configured, ensuring that existing values are not overwritten.
Notes
- Replace
changemewith the target Active Directory group. - Replace
yourdomain.comwith your organization’s email domain. - The account running the script must have permission to modify Active Directory user objects.
- Always test the script against a small pilot group before running it in a production environment.