trying to get group members smtp address and DG types

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)"}

TIA!
exchsvrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chirag NagrekarSystem AnalystCommented:
So you want AD Group Members Name, their email addresses and if AD group is security group or Distribution group as a result ?
exchsvrAuthor Commented:
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 |

ForEach-Object{
            $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 NagrekarSystem AnalystCommented:
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.
oBdACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
exchsvrAuthor Commented:
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 {

    Param(
        $group,
        $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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.