# Validate CSV of SamAccountNames against multiple groups

**URL:** <https://forums.powershell.org/t/validate-csv-of-samaccountnames-against-multiple-groups/12931>\
**Category:** PowerShell Help\
**Created:** [July 29, 2019, 8:23am UTC](https://forums.powershell.org/t/validate-csv-of-samaccountnames-against-multiple-groups/12931 "2019-07-29T08:23:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![powershell-noob](https://avatars.discourse-cdn.com/v4/letter/p/90ced4/32.png) [@powershell-noob](https://forums.powershell.org/u/powershell-noob)\
**Post date:** [July 29, 2019, 8:23am UTC](https://forums.powershell.org/t/validate-csv-of-samaccountnames-against-multiple-groups/12931/1 "2019-07-29T08:23:21Z")

</div>

Hello,

I need to validate a CSV of SANs against multiple group. I only want to return the users who do not belong to ALL of the groups. Currently my code returns everyone and I understand why (it’s only looking at the members of the last group), but not how to fix. I also realize I probably need to create a hashtable for my output to remove the “@{SamAccountName=” in my results. List of CSV may contain 3000 accts if it helps.

EDIT: By not being part of these groups we determine they are no longer in AD, that’s why I’m not using memberOf.

```
$Users=Import-Csv-Path"C:\AdminTools\PowerShell\SamAccountNames.csv"$agroup="group1"$bgroup="group2"$cgroup="group3"$dgroup="group4"$egroup="group5"$fgroup="group6"$Members=Get-ADGroupMember-Identity"$agroup"-Recursive|Select-ExpandPropertysamaccountname$Members=Get-ADGroupMember-Identity"$bgroup"-Recursive|Select-ExpandPropertysamaccountname$Members=Get-ADGroupMember-Identity"$cgroup"-Recursive|Select-ExpandPropertysamaccountname$Members=Get-ADGroupMember-Identity"$dgroup"-Recursive|Select-ExpandPropertysamaccountname$Members=Get-ADGroupMember-Identity"$egroup"-Recursive|Select-ExpandPropertysamaccountname$Members=Get-ADGroupMember-Identity"$fgroup"-Recursive|Select-ExpandPropertysamaccountnameforeach ($userin$users){if ($Members-Contains$User.SamAccountName){#Write-Host "$User in Group"} Else {Write-Host"$User not in group"}

}
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![powershell-noob](https://avatars.discourse-cdn.com/v4/letter/p/90ced4/32.png) [@powershell-noob](https://forums.powershell.org/u/powershell-noob)\
**Post date:** [July 29, 2019, 6:17pm UTC](https://forums.powershell.org/t/validate-csv-of-samaccountnames-against-multiple-groups/12931/2 "2019-07-29T18:17:34Z")

</div>

Seems to be working now. I forgot to use += over = so it doesnt overwrite.

---

<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:31pm UTC](https://forums.powershell.org/t/validate-csv-of-samaccountnames-against-multiple-groups/12931/3 "2024-05-16T20:31:53Z")

</div>


