Home » PowerShell » Get-ADUser Email Address Using PowerShell

Get-ADUser Email Address Using PowerShell

ActiveDirectory module for Windows PowerShell contains a group of cmdlets to manage your Active Directory domains, Configuration sets, manage active directory users like get-aduser email address, get userprincipalname using PowerShell.

In this blog post, we will discuss how to get active directory email address using the PowerShell script. We will use the get-aduser cmdlet to get ad user email address from the list of users and get aduser email address in a CSV file.

The following steps describe how to get-aduser email address:

Import ActiveDirectory PowerShell Module

import-module ActiveDirectory

PowerShell Tip: How to get a list of PowerShell modules!

Get-AdUser Properties from Active Directory

In PowerShell, get aduser properties from active directory, run below command

get-aduser username -properties *

In the above command, provide the username for which you want to get ad user properties from the active directory. On Successful command run, it will return user properties from the active directory

For example, let’s consider an example to get Toms user properties from the active directory, run below command

Get-ADUser Toms -Properties *

Above PowerShell script, get all properties of active directory user and print it on console as below

PowerShell Get-AdUser Properties
PowerShell Get-AdUser Properties

To get aduser email address from active directory

To get aduser email address, displayname, and samaccountname from the active directory, run the below command

Get-ADUser -Filter * -Properties EmailAddress,DisplayName, samaccountname| select EmailAddress, DisplayName

The above command will get ad user email address, display name, and samaccountname. You can define other properties comma-separated in the above command.

The output of the above script in PowerShell get user email address as below

PowerShell Get-Aduser User Email Address
PowerShell Get User Email Address

Cool Tip: How to get aduser job title using PowerShell!

To get ad user email address in csv file

To get-aduser email address, displayname, and samaccountname and export it to CSV, run the below command

Get-ADUser -Filter * -Properties EmailAddress,DisplayName, samaccountname| select EmailAddress, DisplayName, samaccountname | Export-CSV D:\PowerShell\Ad_user_email_Address.csv"

Above Get-ADUser command return user email address, display name, and samaccountname and export all user properties from active directory to CSV file.

Export-CSV cmdlet export users and email addresses from the active directory to the CSV file on the specified location.

PowerShell Get AdUser Email Address from SamAccountName

In PowerShell, to get aduser email address from a list of users having samaccountname, run the below command

$users = Get-Content .\samaccountname.txt
$users | ForEach-Object {
    Get-ADUser -Identity $_ -properties mail | Select samaccountname,mail
} | Export-CSV aduserEmails.txt -NoTypeInformation

In the above PowerShell script, the first command uses the Get-Content cmdlet to read the samaccountname.txt file having a list of users and pass its output to the second command.

Second command iterates over the list of $users using ForEach-Object to get aduser email address and samaccountname and pass its output to the third command.

The third command uses the Export-Csv cmdlet to export users and email addresses from the active directory to the CSV file.

The output of get Ad User email address from a list of users having samaccountname in CSV file as below

"samaccountname","mail"
"toms","[email protected]"
"ErickJ","[email protected]"
"garyw","[email protected]"
"chrisd","[email protected]"
"adam","[email protected]"

Get AdUser by Email Address in PowerShell

If you want to Get-Aduser by email address in PowerShell, run the below command

Get-ADUser -Filter {Emailaddress -eq '[email protected]'}

In the above PowerShell script, Get-AdUser Filter parameter check Emailaddress equal to the specified email address and get ad user from email address as below

PS C:\Windows\system32> Get-ADUser -Filter {Emailaddress -eq '[email protected]'}


DistinguishedName : CN=Tom Smith,OU=SALES,DC=SHELLPRO,DC=LOCAL
Enabled           : True
GivenName         : Tom
Name              : Tom Smith
ObjectClass       : user
ObjectGUID        : 1f3a2572-2621-4e47-9bdf-81d1f8172f69
SamAccountName    : toms
SID               : S-1-5-21-1326752099-4012446882-462961959-1103
Surname           : Smith
UserPrincipalName : [email protected]

PowerShell Get Email Address from Display Name

To get an email address from display name, use the Get-AdUser Filter parameter to check where DisplayName is equal to provided displayname and get aduser from the active directory.

For example, you have a list of adusers in CSV file as format below

"DisplayName"
"Tom Smith",
"Erick Jones",
"Gary Willy",
"Chirs.Dore"

To get email address of aduser by displayname, run below PowerShell script

# Import csv file and read display names of user
$UserNames = Import-Csv -Path D:\AdusersList.csv
foreach ($un in $UserNames)
{
    $displayName = $un.DisplayName.Replace(',', ' ')
    Write-Host $displayName
    # Get email address from display name using filter parameter
    Get-Aduser -Filter "DisplayName -eq '$displayName'" -Properties EmailAddress, mail
}

In the above PowerShell script,

Use Import-Csv the cmdlet to read aduserlist.csv file on the specified location, get a list of ad users and store it $UserNames variable

Use foreach to iterate over users display names and use the Get-AdUser filter parameter to check if user display name equal to provided display name and get email address from list of users display name.

Get AdUser Filter Email Address Empty

You can get aduser having email address empty, using the Get-AdUser filter email address condition as given below

Get-ADUser -Filter {mail -notlike '*'} -Property EmailAddress  | Select Name,EmailAddress

In the above PowerShell script, it checks if the email address is empty and returns the list of users.

Cool Tip: How to use Get-AdOrganizationalUnit in the Active Directory!

Get AdUser Filter by Email Address not Null

You can get aduser filter by email address not null using the below script

Get-ADUser -Filter * -Property EmailAddress | Where { $_.EmailAddress -ne $null }| Select Name,EmailAddress

In the above PowerShell script, it checks where aduser emailaddress is not null and returns the list of users.

Get AdUser Manager Email Address

If you want to get aduser manager email address, use the given below script

$user = "garyw"
$Manager = get-aduser $user -properties * | Select -ExpandProperty Manager

get-aduser $Manager -properties * | Select EmailAddress

In the above PowerShell script,

$user variable stores user name.

The second command uses Get-AdUser to get aduser manager object by providing $user.

The third command uses the Get-AdUser command to get aduser manager email address.

Cool Tip: How to get an active directory user permission report!

Conclusion

I hope the above step-by-step guide on how to get ad user email address using PowerShell script will help you to get aduser account properties.

As mentioned in the above article, you can define properties by comma-separated and use the Export-CSV cmdlet to export the list of users and email addresses from the active directory to the CSV file.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment