# Comparing users in an AD group to a CSV file

**URL:** <https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512>\
**Category:** PowerShell Help\
**Created:** [June 3, 2022, 5:17pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512 "2022-06-03T17:17:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sajed1001](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@sajed1001](https://forums.powershell.org/u/sajed1001)\
**Post date:** [June 3, 2022, 5:17pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512/1 "2022-06-03T17:17:39Z")

</div>

Hello everyone!  
I wanted to compare a list of users I have in a CSV to see if the Users exists in an AD group. My CSV contains emailaddress, name as headers. My ad group is say ABC  
I tried using two ways - 1. using compare-object and 2. using foreach loop. Both ways didnt seem to work.

1. Using compare object

```auto
$com = Import-Csv -path "./test.csv"
$users = Get-ADGroupMember -Identity "ABC"
$csvName = $com.Name
$usersName = $users.Name

$compare = Compare-Object $usersName $csvName -IncludeEqual -ExcludeDifferent | sort -Property InputObject

$compare

```

1. Using Foreach

```auto
$com = import-csv -path ".\test.csv"
$users = Get-ADGroupMember -Identity "ABC"
foreach ($t in $com){
if($com.name -eq $users.name)
    {
    "match"
    }
}

```

Any suggestions will be really aprpeciated!

Thank you!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 3, 2022, 6:14pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512/2 "2022-06-03T18:14:42Z")

</div>

Since you have a property `Name` in your CSV data I’d expect something like this to work actually:

```auto
$com = Import-Csv -path "./test.csv"
$users = Get-ADGroupMember -Identity "ABC"
Compare-Object -ReferenceObject $com -DifferenceObject $users -Property Name -IncludeEqual

```

But therefor the properties `Name` from your CSV file have to be exactly the same like they are in your AD.

Since the AD attribute `Name` is not considered to be unique I’d recommend to use the sAMAccountName instead. 👆🏽

---

<div class="post-metadata">

**Author:** ![sajed1001](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@sajed1001](https://forums.powershell.org/u/sajed1001)\
**Post date:** [June 3, 2022, 7:15pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512/3 "2022-06-03T19:15:33Z")

</div>

Hi Olaf,  
I am trying to get the samaccount name from email addresses in the CSV. My csv contains emailadress and name column. USERID is the emailaddress column

```auto
$test1 = Import-Csv -path ".\test.csv" -Header USERID
ForEach($t in $test1)
{
Get-ADUser -Filter "UserPrincipalName -like '$($test1.USERID)'" | Select-Object -ExpandProperty samAccountName
}

```

This is isnt giving me the samaccount name which is very odd.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 3, 2022, 7:25pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512/4 "2022-06-03T19:25:52Z")

</div>

Something like this happens when you use inappropriate varialbe names like `$test1` and `$t`. 😉 You refenced the array inside your loop instead of the single elements.

I’d do it this way:

```auto
$UserList = Import-Csv -Path ".\test.csv" -Header USERID
$ResultList = 
ForEach ($User in $UserList) {
    Get-ADUser -Filter "UserPrincipalName -eq '$($User.USERID)'"
}
$ResultList.sAMAccountName

```

---

<div class="post-metadata">

**Author:** ![sajed1001](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@sajed1001](https://forums.powershell.org/u/sajed1001)\
**Post date:** [June 3, 2022, 7:34pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512/5 "2022-06-03T19:34:57Z")

</div>

It is truly Friday. I replaced $test1.USERID with $t1.USERID and it worked. Going to try using Samaccountname to see if the compare works. Thank you!

---

<div class="post-metadata">

**Author:** ![sajed1001](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@sajed1001](https://forums.powershell.org/u/sajed1001)\
**Post date:** [June 3, 2022, 8:17pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512/6 "2022-06-03T20:17:29Z")

</div>

Olaf,  
Would you happen to know why is the result not exporting into a CSV but only exporting the length into the CSV?

```auto
$test1 = Import-Csv -path ".\test.csv" -Header USERID
ForEach($t in $test1)
{
Get-ADUser -Filter "UserPrincipalName -eq '$($t1.USERID)'" | Select-Object -ExpandProperty samAccountName | Export-csv -path ".\names.csv"
}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 3, 2022, 9:00pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512/7 "2022-06-03T21:00:06Z")

</div>

In PowerShell we work with rich and powerful objects and properties. When you use `Select-Object -ExpandProperty` you turn the output into boring stupid text/strings.

I’d recommend to review the code snippet I posted and use a variable to catch the output of the foreach loop completely. You can use this variable later on to do whatever steps you need to do - including outputting only the sAMAccountName to a CSV file like this:

```auto
$ResultList | 
    Select-Object -Property sAMAccountName |
        Export-Csv -Path '.\sAMAccountNameList.csv' -NoTypeInformation

```

---

<div class="post-metadata">

**Author:** ![sajed1001](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@sajed1001](https://forums.powershell.org/u/sajed1001)\
**Post date:** [June 3, 2022, 10:43pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512/8 "2022-06-03T22:43:04Z")

</div>

Thank you, Olaf! I am learning new things in powershell everyday! That worked!

---

<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:17pm UTC](https://forums.powershell.org/t/comparing-users-in-an-ad-group-to-a-csv-file/19512/9 "2024-05-16T20:17:53Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


