Grab users list with email alias and add AD attributes help

Hello All! I’m new to the ps world, mostly python before .

I need help and this is likely very simple for you all.

I need a script to grab a .csv or xls file with unique users email alias, then use that to identify users in AD and gather their additional AD attributes such as ‘Office’, ‘Job title’ and ‘department’, then export that data into a new csv.

Can someone please help? Really appreciate the time!

this is what I was thinking and had in mind:

$userEmails = Import-Csv ‘C:\PS\Input\Emails.txt’
$adUsers = @()
$regex = [regex]“[Oo][Cc][Pp][Ss][0-9]”

foreach ($email in $userEmails)
{
#$emailAdd = $email.RecipientAddress
$userID = $email.ID
if($emailAdd.split(“@”)[1] -eq “company.net”)
{
# $adUsers += Get-ADuser -Filter {EmailAddress -eq $emailAdd} -Properties *
$adUsers += Get-ADuser $userID -Properties Title, Office, EmailAddress
}
else
{

}

Badner,
Welcome to the forums.

Please format your code as code here in the forum. Simply click the “</>” symbol in the edit bar and paste youre code.

Assumed you have a proper CSV file with a comma as the delimiter and at least the header named “EmailAddress” you could start with something like this.

$UserEmailList = 
Import-Csv -Path 'C:\PS\Input\Emails.csv' -Delimiter ','

$Result =
foreach ($UserEmail in $UserEmailList) {
        Get-ADUser -Filter "Mail -eq '$($UserEmail.EmailAddress)'" -Properties Title, Office, EmailAddress, Mail  |
            Select-Object -Property Name, sAMAccountName, DisplayName, Title, Office, EmailAddress, Mail, @{Name = 'InputUserEmail'; Expression = {$UserEmail}}
}
$Result
$Result | Export-Csv -Path 'C:\PS\Input\Result.csv' -Delimiter ',' -NoTypeInformation

…of course untested :wink:

Just to have the confirmation which input email address fits to the output emailaddress I included the input email address in the output again. Of course you may add error handling to catch any error occuring when you have an input email address not available in your AD.