Check Email Addresses Listed in Active Directory

PowerShell - @SeniorDBA

One of the tasks that administrators often need to perform is to verify that each active directory user account has a valid email address. This is important for ensuring that users can receive notifications, access online services, and communicate with other users. There are different ways to verify the email addresses of active directory users, but in this article, we will focus on one method that uses PowerShell.

PowerShell is a scripting language that allows administrators to automate tasks and manage systems. PowerShell can interact with active directory through the ActiveDirectory module, which provides cmdlets for querying and modifying objects in the directory. To use PowerShell to verify the email addresses of active directory users, we need to follow these steps:

  1. Install the ActiveDirectory module on the computer where we want to run the script. We can do this by opening PowerShell as an administrator and running the command: Install-Module -Name ActiveDirectory
  2. Import the ActiveDirectory module into the current PowerShell session by running the command: Import-Module -Name ActiveDirectory
  3. Get a list of all active directory user accounts by running the command: $users = Get-ADUser -Filter *
  4. Loop through each user account and check if it has a valid email address by running the following code:
$users = Get-ADUser -Filter *
foreach ($user in $users) {
# Check if the user has an email address attribute
if ($user.EmailAddress) {
# Check if the email address is valid using a regular expression
$pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
if ($user.EmailAddress -match $pattern) {
# The email address is valid, write a message to the console
Write-Host "$($user.SamAccountName) has a valid email address: $($user.EmailAddress)"
}
else {
# The email address is invalid, write a warning to the console
Write-Warning "$($user.SamAccountName) has an invalid email address: $($user.EmailAddress)"
}
}
else {
# The user does not have an email address attribute, write an error to the console
Write-Error "$($user.SamAccountName) does not have an email address"
}
}

This code will output a message for each user account, indicating whether it has a valid, invalid, or no email address. We can also redirect the output to a file or send it as an email report if we want. You might also check to see if they are all from the same domain, or flag addresses that don’t end with the proper domain.

By using PowerShell and the ActiveDirectory module, we can easily verify the email addresses of active directory users and identify any issues that need to be fixed.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.