Combining Get-ADGroupMember with Get-User

Welcome Forums General PowerShell Q&A Combining Get-ADGroupMember with Get-User

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
3 weeks ago.

  • Author
    Posts
  • #158273

    Participant
    Topics: 3
    Replies: 5
    Points: 48
    Rank: Member

    Hi All,

    We have an Office 365 / On Premise Exchange hybrid environment. We have an active directory group and I am looking for a way to know the members of the group have On Premise Mailbox or Office 365 Mailbox. Usually we run below AD Power Shell script to get the group members and then run the second Exchange Power Shell to know the recipient type. Is there any way to combine both and run a single script from Exchange Shell ?

    Get-ADGroupMember -Identity "Group Name" | Get-ADObject -Properties Name, DisplayName | Select-Object Name, DisplayName | Export-Csv C:\Temp\List.csv -NoType

    Get-Content "C:\Temp\List.csv" | Get-User | Select-Object Name, DisplayName, UserPrincipalName, WindowsEmailAddress, RecipientType | Export-Csv C:\Temp\UserExInfo.csv -NoTypeInformation

     

    Thanks

    Rajesh

  • #158313

    Participant
    Topics: 2
    Replies: 957
    Points: 1,730
    Helping Hand
    Rank: Community Hero

    Just ask AD for the groups with Get-ADGroup, or pass that name in manually of from a CSV, then pass the group by name to Get-ADGroupMember, then pass the member from the group by name to the Get-User and export as needed.

    Clear-Host
    # Get all groups 
    (Get-ADGroup -Filter '*').SamAccountName | 
    ForEach {
        # Process each group to get members
        "`n*** Process $PSItem group`n"
        ForEach($User in (Get-ADGroupMember -Identity $PSItem))
        {
            # Process each member to get mailbox user type and data
            Get-User -Identity $User.SamAccountName -ErrorAction SilentlyContinue | 
            Select-Object -Property RecipientTypeDetails, Name, DisplayName, UserPrincipalName
        }
    } 
    # Results
      RecipientTypeDetails Name                        DisplayName                  UserPrincipalName
      -------------------- ----                        -----------                  -----------------
    *** Process Domain Users group
    
               UserMailbox Administrator               Administrator                Administrator@contoso.com
    ...
              DisabledUser krbtgt
    ...
    ...                                      
         RemoteUserMailbox ExoAdmin ServiceAccount     ExoAdmin ServiceAccount      exoadmin@contoso.com
    ...
    
  • #158333

    Participant
    Topics: 3
    Replies: 5
    Points: 48
    Rank: Member

    Thanks a lot for the support. Since we are looking for only one group, i have shortened the script as below.

    ForEach($User in (Get-ADGroupMember -Identity ADGroup)) {Get-User -Identity $User.SamAccountName -ErrorAction SilentlyContinue | Where RecipientTypeDetails -eq UserMailbox | Select-Object -Property Name, DisplayName, UserPrincipalName, WindowsEmailAddress }

    But when i tried to export the results as CSV, I am getting below error.

    ForEach($User in (Get-ADGroupMember -Identity ADGroup)) {Get-User -Identity $User.SamAccountName -ErrorAction SilentlyContinue | Where RecipientTypeDetails -eq UserMailbox | Select-Object -Property Name, DisplayName, UserPrincipalName, WindowsEmailAddress } | Export-Csv C:\Temp\UserExInfo-$(Get-Date -Format "ddMMMyyyy").csv -NoTypeInformation

    At line:1 char:281
    + ... PrincipalName} | Export-Csv C:\Temp\UserExInfo-$(Get-Date -Format "ddMMMyyyy").c ...
    + ~
    An empty pipe element is not allowed.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement

     

    Any idea whats wrong ?

     

    Thanks

    Rajesh

  • #158351

    Participant
    Topics: 1
    Replies: 111
    Points: 92
    Rank: Member

    The ForEach loop, unfortunately, cannot pass objects down the pipeline. Try using a variable assignment for the ForEach loop then call the resultant variable and pipe to Export-Csv.

    
    $var=ForEach($x in $y){$x}
    
    $var | Export-Csv path/to/csv
    
    

     

  • #158361

    Participant
    Topics: 3
    Replies: 5
    Points: 48
    Rank: Member

    Thanks a lot, it worked.

  • #158384

    Participant
    Topics: 2
    Replies: 957
    Points: 1,730
    Helping Hand
    Rank: Community Hero

    I see you already got a follow-up, but with mine, it doe into matter how you collect your groups, as long as you pass just that name.

    So, you could have just made one change.

    # This way 
    
    Clear-Host
    # Use the selected group 
    (Get-ADGroup -Filter '*').SamAccountName -eq 'Domain Users' | 
    ForEach {
        # Process each group to get members
        "`n*** Process $PSItem group`n"
        ForEach($User in (Get-ADGroupMember -Identity $PSItem))
        {
            # Process each member to get mailbox user type and data
            Get-User -Identity $User.SamAccountName -ErrorAction SilentlyContinue | 
            Select-Object -Property RecipientTypeDetails, Name, DisplayName, UserPrincipalName
        }
    }
    
    # or this way ...
    
    Clear-Host
    # Use this group
    'Domain Users' | 
    ForEach {
        # Process each group to get members
        "`n*** Process $PSItem group`n"
        ForEach($User in (Get-ADGroupMember -Identity $PSItem))
        {
            # Process each member to get mailbox user type and data
            Get-User -Identity $User.SamAccountName -ErrorAction SilentlyContinue | 
            Select-Object -Property RecipientTypeDetails, Name, DisplayName, UserPrincipalName
        }
    }
    

You must be logged in to reply to this topic.