Need help finding users in multiple ad groups in single list

I am tasked with getting a complete list of users from two different AD Groups. i.e. Group-A “VPN” and are also in Group-B “DEN”. I would also like to export the list with the name of the user and email address. Is this possible? I did see previous postings like this:

$groups = "VPN","DEN"

$resultsarray =@()
foreach ($group in $groups) {
$resultsarray += Get-ADGroupMember -Id $group | select samaccountname,name,@{Expression={$group};Label="Group Name"}


But it outputs into two separate lists.

samaccountname   name    Group Name 
--------------    ----    ---------- 
usera    Test User A    VPN
userb    Test User B    VPN
usera    Test User A    DEN
userb    Test User B    DEN


I would like to output all on one line, something like this:

samaccountname   name    Group A     Group B 
--------------    ----    ---------- ---------
usera    Test User A    VPN    DEN
userb    Test User B    VPN    DEN

Any assistance would be much appreciated!!!


If the users need to be a member of BOTH groups, why are you listing what groups they are in? You can filter like so and find users that are a member of both groups:

 $users = Get-ADUser -Properties Mail -Filter {(MemberOf -eq 'CN=SG - AppX,OU=Groups,OU=Demo,DC=DEMO,DC=LOCAL') -and  (MemberOf -eq 'CN=SG - VPN,OU=Groups,OU=Demo,DC=DEMO,DC=LOCAL')} |
         Select Name,