# Get-Emails from AD

**URL:** <https://forums.powershell.org/t/get-emails-from-ad/8109>\
**Category:** PowerShell Help\
**Created:** [February 21, 2017, 11:50am UTC](https://forums.powershell.org/t/get-emails-from-ad/8109 "2017-02-21T11:50:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 21, 2017, 11:50am UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/1 "2017-02-21T11:50:48Z")

</div>

Hello everyone,

need your help,  
i have a list with emails(some of this users still in ad some left)  
i need find information based on this emails, i need the email associated to user who not in AD still stay in report with error message  
my script

```
$csv = get-content "c:\Email.csv"
$results = foreach($mail in $csv){
    $userMail = $user.mail
    Get-ADUser -Filter {mail -eq $mail} -Properties Name, mail,Description,Title,Manager | Select-Object Name, mail,Description,Title,Manager 

}$results | Export-Csv "c:\Email_Report.csv" -NoTypeInformation
```

this part working ok, but it removing from report email which not exist in AD, but i need them in report

Thanks.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [February 21, 2017, 12:49pm UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/2 "2017-02-21T12:49:26Z")

</div>

There are a lot of ways to do things. First, your loop was incorrect. You were using foreach ($mail in $csv) and referencing $userMail as $user.Mail and then your filter as $mail, which would be an object unless you did $mail.Mail.

The updated code will create an object if the user was found or not found in AD. Status was added to quickly identify if finding the user was Successful or Failed, but you could even just use Name to see if is $null to see which failed.

```
$csv = get-content "c:\Email.csv"

$results = foreach($user in $csv){
    $userMail = $user.mail
    $adUser = Get-ADUser -Filter {mail -eq $userMail} -Properties Name, mail,Description,Title,Manager
               
    if ($adUser) {
        $adUser |
        Select-Object Name, 
                      mail,
                      Description,
                      Title,
                      Manager,
                      @{Status="Status";Expression={"Success"}}
    }
    else {
        New-Object -TypeName PSObject -Property @{
            Name = $null
            mail = $userMail
            Description = $null
            Title = $null
            Manager = $null
            Status = "Failed"
        }
    }
}

$results | Export-Csv "c:\Email_Report.csv" -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 21, 2017, 1:09pm UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/3 "2017-02-21T13:09:45Z")

</div>

Hi Rob,

Thank you for example, but i get the error:

```
Get-ADUser : Variable: 'userMail' found in expression: $userMail is not defined.
At line:5 char:15
+ ... $adUser = Get-ADUser -Filter {mail -eq $userMail} -Properties Name, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : Variable: 'userMail' found in expression: $userMail is not defined.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 
Select-Object : The Status key is not valid.
At line:9 char:9
```

---

<div class="post-metadata">

**Author:** ![skalizar](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@skalizar](https://forums.powershell.org/u/skalizar)\
**Post date:** [February 21, 2017, 1:34pm UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/4 "2017-02-21T13:34:45Z")

</div>

From your first script, I assume that this file is not actually a csv, just a text file with a list of email addresses.

If so remove this line.

$userMail = $user.mail

Change this line.

$results = foreach($userMail in $csv){

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 21, 2017, 1:44pm UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/5 "2017-02-21T13:44:17Z")

</div>

Hi Ron,

Thank you again,but still error:

```
Select-Object : The Status key is not valid.
At line:10 char:9
+ Select-Object Name,
+ ~~~~~~~~~~~~~~~~~~~
```

and i changed script

```
$results = foreach($userMail in $csv){
#$userMail = $user.mail
$adUser = Get-ADUser -Filter {mail -eq $userMail} -Properties Name, mail,Description,Title,Manager
```

---

<div class="post-metadata">

**Author:** ![skalizar](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@skalizar](https://forums.powershell.org/u/skalizar)\
**Post date:** [February 21, 2017, 2:08pm UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/6 "2017-02-21T14:08:26Z")

</div>

Try this.

@{ Name = “Status”; Expression = { “Success” } }

It doesn’t like Status=“Status”

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 21, 2017, 2:18pm UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/7 "2017-02-21T14:18:46Z")

</div>

Hi Ron,

Thank you  
its still give me error:

```
Get-ADUser : The search filter cannot be recognized
At line:6 char:15
+ ... $adUser = Get-ADUser -Filter {mail -eq $userMail} -Properties Name, ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

but this time it create report  
Thanks.

---

<div class="post-metadata">

**Author:** ![skalizar](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@skalizar](https://forums.powershell.org/u/skalizar)\
**Post date:** [February 21, 2017, 2:22pm UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/8 "2017-02-21T14:22:02Z")

</div>

Can you show me a sample of the file? Be sure obfuscate any sensitive info.

---

<div class="post-metadata">

**Author:** ![brad-sam](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@brad-sam](https://forums.powershell.org/u/brad-sam)\
**Post date:** [February 21, 2017, 2:25pm UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/9 "2017-02-21T14:25:13Z")

</div>

Ron - Thank you again  
i find my problem  
i try to read data from csv file, i change it on txt and all work

Thanks again.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/get-emails-from-ad/8109/10 "2024-05-16T20:39:13Z")

</div>


