Need help finding users in multiple ad groups in single list

Welcome Forums General PowerShell Q&A Need help finding users in multiple ad groups in single list

Viewing 1 reply thread
  • Author
    Posts
    • #214293
      Participant
      Topics: 1
      Replies: 0
      Points: 11
      Rank: Member

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

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

       

    • #214434
      Participant
      Topics: 12
      Replies: 1607
      Points: 2,495
      Helping Hand
      Rank: Community Hero

      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,
                      UserPrincipalName,
                      SamAccountName,
                      Mail
      
      $users
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.