Comparing users in an AD group to a CSV file

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
$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
$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!

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

$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. :point_up_2:t4:

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

$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.

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

I’d do it this way:

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

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!

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

$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"
}

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:

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

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