trying to get group members smtp address and DG types

exchsvr used Ask the Experts™
In the query below, i am try to import AD dist groups(both distribution and security groups)  from a CSV, and would like to return the user name and email address and indicate whether the groups is regular distribution or a security group.  Is it possible to get these results?

Get-ADGroupMember "dist.grp" | select samaccountname | %{Get-ADUser $_.samaccountname -Properties mail} | %{write-output "$($_.samaccountname) `t`t  $($_.mail)"}

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chirag NagrekarAssociate - Sys Admin

So you want AD Group Members Name, their email addresses and if AD group is security group or Distribution group as a result ?


Hi Chirag,

Yes, as we need to split distribution from security groups. I can get the user and SMTP address now, but having a problem with displaying the group types;

Get-content C:\groups.txt |

            $groupname = $_
            Get-ADGroupMember $groupname -Recursive |
                  Where{$_.objectClass -eq 'User'} |
                  Get-ADUser -Properties samaccountname, mail |
                  Select *,@{n='GroupName';e={$groupname}}
      } |

Select GroupName, samAccountName, mail | Export-csv "C:\output.csv" -NoType
Chirag NagrekarAssociate - Sys Admin

You can use this command to get Group Category

(Get-ADGroup $groupname).GroupCategory

And later create PS Custom Object to merge all outputs and export them into CSV.
Most Valuable Expert 2018
Distinguished Expert 2018
This will return what you're currently planning for.
But you're aware that this will not get you the full truth, because you're doing a -Recursive enumeration of the group members, so you'll only get the category and group membership of the "top level" group you passed? Nested members will not be listed under their real group  membership and category.
Get-Content -Path C:\groups.txt | ForEach-Object {
	$group = Get-ADGroup -Identity $_
	$group | Get-ADGroupMember -Recursive |
		Where-Object {$_.objectClass -eq 'User'} |
		Get-ADUser -Properties mail |
		Select-Object -Property @{n='GroupName'; e={$group.Name}}, @{n='GroupCategory'; e={$group.GroupCategory}}, SamAccountName, Mail 
} | Export-Csv "C:\output.csv" -NoTypeInformation

Open in new window


hi oBda,

thank you, the script you shared is exactly what we need.  In order to get nested groups, i have a separate script, is there any way to add this to the existing?
Function Get-ADGroupMemberRecursive {

        $chain = @()

    $Members = @()
    $chain += $group

    $Members += Get-ADGroup $group |
        Get-ADGroupMember |
            Select-Object -Property `
                @{n='Group';e={$group}},Name,SamAccountName,objectClass,@{n='Chain';e={$chain -join " -> "}}

    $Members += ForEach ($Member in ($Members | Where-Object {$_.objectClass -eq 'group' -and $chain -notcontains $_.Name})){
        Get-ADGroupMemberRecursive $Member.Name $chain


    Return $Members


$List = ForEach ($Group in (Get-Content 'C:\groups.txt')){

    Get-ADGroupMemberRecursive $Group |
    ForEach-Object {
        If($_.objectClass -eq 'user') {
            $u = Get-ADUser $_.samaccountname
            $_ = $_ | Select-Object -Property *,@{n='SurName';e={$u.surname}},@{n='GivenName';e={$u.givenname}}

$List | Export-CSV output.csv -NoTypeInformation

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial